Some bash script stuff - syntax error

i am using the sway compositor and i just discovered this awesome command so i want to make an alias of it. i add these lines on my ~/.bashrc:

_swayfloat() 
exec swaymsg -t get_tree |jq -r '..|try select(.type=="floating_con")|"\(.rect.width)x\(.rect.height) \t\(.id)\t\(.name)"'

alias swfl='_swayfloat'

yet it seems it doesnt work cause every time i open a new kitty window i get this error:

bash: /home/systemdeeznuts/.bashrc: line [--]: syntax error near unexpected token `exec'
bash: /home/systemdeeznuts/.bashrc: line [--]: `exec swaymsg -t get_tree |jq -r '..|try select(.type=="floating_con")|"\(.rect.width)x\(.rect.height) \t\(.id)\t\(.name)"''

i tried some other variations yet i always get a syntax error.
if i just type on a terminal,

[systemdeeznuts@hostname ~]$ exec swaymsg -t get_tree |jq -r '..|try select(.type=="floating_con")|"\(.rect.width)x\(.rect.height) \t\(.id)\t\(.name)"'

it works perfectly fine.

is there a workaround for these types of syntax errors?

Try without the exec?

alias swfl='_swayfloat'

or take the single quotes off…

alias my_test="swaymsg -t get_tree |jq -r \'..|try select(.type==\"floating_con\")|\"\(.rect.width)x\(.rect.height) \t\(.id)\t\(.name)\"\'"

Or escape the quotes…

tried the second one and:

$ my_test 
bash: syntax error near unexpected token `('

At work, no access to a linux box, will have a look tonight.

You don’t need an alias and a function. Just use one or the other.

If you want to use a function, you could do this:

swfl() {
    exec swaymsg -t get_tree |jq -r '..|try select(.type=="floating_con")|"\(.rect.width)x\(.rect.height) \t\(.id)\t\(.name)"'
}

your function does not contian open or closing braces

should look similar
_swayfloat() {
exec swaymsg -t get_tree |jq -r ‘…|try select(.type==“floating_con”)|“(.rect.width)x(.rect.height) \t(.id)\t(.name)”’
}

1 Like