You are not logged in.

#1 2025-06-05 17:29:59

EgoRed
Member
Registered: 2025-05-17
Posts: 5

[SOLVED] Bash script doesn't change env variable when called by sxhkd

I have a simple bash script for turning screen on/off. Here it is. File brightness-toggle.sh:

#!/bin/bash

if ! [[ -v SCREEN_TOGGLE ]]; then
   export SCREEN_TOGGLE=1
fi

if [[ $SCREEN_TOGGLE = 1 ]]; then
   brightnessctl -s; brightnessctl set 0%;  SCREEN_TOGGLE=0
elif [[ $SCREEN_TOGGLE = 0 ]]; then
   brightnessctl -r; SCREEN_TOGGLE=1
fi

It is set for hotkey in my .sxhkdrc file:

#
# settings for  fn+f keys
#

XF86MonBrightnessUp
	brightnessctl set +5%

XF86MonBrightnessDown
	brightnessctl set 5%-

XF86Display
   brightness-toggle.sh

XF86AudioRaiseVolume
   increase-volume.sh
#   pactl set-sink-volume 0 +5%

XF86AudioLowerVolume
	pactl set-sink-volume 0 -5%

XF86AudioMute
	pactl set-sink-mute 0 toggle

XF86Bluetooth
	blueberry

However, when I use it from hotkey, it can't change value of $SCREEN_TOGGLE, thus it doesn't work properly (but it does change brightness though). Also, when I use it directly from terminal it only works properly when I call it as

. brightness-toggle.sh

Trying to run script from sxhkd with "." before it didn't solve the problem.
This may be very stupid question but I would appreciate help with it.

Last edited by EgoRed (2025-06-05 20:30:51)

Offline

#2 2025-06-05 18:23:18

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,153

Re: [SOLVED] Bash script doesn't change env variable when called by sxhkd

This isn't going to work that way. You can't export your shell environment up the chain into sxhkd, which is the process that would have to know about this. Create a file in /tmp and reading the true or false value from there (or just checking for existence of the file and removing it again)  is likely more fruitful for what you're attempting.

It works in the terminal because the terminal itself is changing it's own environment. Once your script has completed any environments you're exporting there will be gone.

Last edited by V1del (2025-06-05 18:25:51)

Offline

#3 2025-06-05 18:38:17

EgoRed
Member
Registered: 2025-05-17
Posts: 5

Re: [SOLVED] Bash script doesn't change env variable when called by sxhkd

V1del wrote:

This isn't going to work that way. You can't export your shell environment up the chain into sxhkd, which is the process that would have to know about this. Create a file in /tmp and reading the true or false value from there (or just checking for existence of the file and removing it again)  is likely more fruitful for what you're attempting.

It works in the terminal because the terminal itself is changing it's own environment. Once your script has completed any environments you're exporting there will be gone.

Thank you so much for the explanation! I will definitely try this approach with temp file.

Offline

#4 2025-06-05 19:28:41

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 75,277

Re: [SOLVED] Bash script doesn't change env variable when called by sxhkd

Skip the state, query "brightnessctl g", if that's the equivalent of "0%" (you'll get an absolute value), restore the previous value.
Are you btw. looking for "xset dpms force off"?

Offline

#5 2025-06-05 20:11:50

EgoRed
Member
Registered: 2025-05-17
Posts: 5

Re: [SOLVED] Bash script doesn't change env variable when called by sxhkd

seth wrote:

Skip the state, query "brightnessctl g", if that's the equivalent of "0%" (you'll get an absolute value), restore the previous value.

I was just thinking about it. Although using separate variable may allow me to extend this script in the future to consider separately is screen on/of and it's brightness.

seth wrote:

Are you btw. looking for "xset dpms force off"?

But maybe that is exactly the thing I am thinking about, thanks!

Offline

Board footer

Powered by FluxBB