Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of "${1#*-}" in BASH

Tags:

bash

There is a if condition as below

if [ "${1#*-}" = "$1" ]; then
   echo "Do something"
fi

But could somebody explain what is the meaning of ${1#*-} ?

like image 606
rɑːdʒɑ Avatar asked Jan 23 '17 12:01

rɑːdʒɑ


People also ask

What does ${ 1 mean in shell script?

It says, “If there is at least one argument ( ${1+ ), then substitute in all the arguments ( “$@” ) preserving all the spaces, etc. within each argument.

What does$ 1 mean in linux?

$1 is the first command-line argument passed to the shell script. Also, know as Positional parameters. For example, $0, $1, $3, $4 and so on. If you run ./script.sh filename1 dir1, then: $0 is the name of the script itself (script.sh)

What does$ 0 mean in bash?

From Bash Manual. $0 Expands to the name of the shell or shell script. This is set at shell initialization. If Bash is invoked with a file of commands (see Section 3.8 [Shell Scripts], page 39), $0 is set to the name of that file.

What does #! Mean in bash?

#!/bin/bash The most common shebang is the one referring to the bash executable: #!/bin/bash. Essentially it tells your terminal that when you run the script it should use bash to execute it.


1 Answers

${1#*-} deletes the shortest match of *-, a glob-like pattern from $1 variable.

E.g. abcdef-xyz-foo -> xyz-foo

Your if check does actually:

if $1 does not contain '-'
like image 123
Kent Avatar answered Nov 09 '22 23:11

Kent