Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash "command not found" error

Tags:

bash

unix

I am trying to edit my .bashrc file with a custom function to launch xwin. I want it to be able to open in multiple windows, so I decided to make a function that accepts 1 parameter: the display number. Here is my code:

function test(){
    a=$(($1-0))
    "xinit -- :$a -multiwindow -clipboard &"
}

The reason why I created a variable "a" to hold the input is because I suspected that the input was being read in as a string and not a number. I was hoping that taking the step where I subtract the input by 0 would convert the string into an integer, but I'm not actually sure if it will or not. Now, when I call

test 0

I am given the error

-bash: xinit -- :0 -multiwindow -clipboard &: command not found

How can I fix this? Thanks!

like image 423
boof Avatar asked Sep 01 '26 04:09

boof


2 Answers

Because the entire quoted command is acting as the command itself:

$ "ls"
a b c
$ "ls -1"
-bash: ls -1: command not found

Get rid of the double quotation marks surrounding your xinit:

xinit -- ":$a" -multiwindow -clipboard &
like image 146
bishop Avatar answered Sep 04 '26 12:09

bishop


In addition to the double-quotes bishop pointed out, there are several other problems with this function:

  • test is a standard, and very important, command. Do not redefine it! If you do, you risk having some script (or sourced file, or whatever) run:

    if test $num -eq 5; then ...
    

    Which will fire off xinit on some random window number, then continue the script as if $num was equal to 5 (whether or not it actually is). This way lies madness.

  • As chepner pointed out in a comment, bash doesn't really have an integer type. To it, an integer is just a string that happens to contain only digits (and maybe a "-" at the front), so converting to integer is a non-opertation. But what you might want to do is check whether the parameter got left off. You can either check whether $1 is empty (e.g. if [[ -z "$1" ]]; then echo "Usage: ..." >&2 etc), or supply a default value with e.g. ${1:-0} (in this case, "0" is used as the default).

  • Finally, don't use the function keyword. bash tolerates it, but it's nonstandard and doesn't do anything useful.

So, here's what I get as the cleaned-up version of the function:

launchxwin() {
    xinit -- ":${1:-0}" -multiwindow -clipboard &
}
like image 26
Gordon Davisson Avatar answered Sep 04 '26 12:09

Gordon Davisson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!