$0
é apenas uma variável bash interna. De man bash
:
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, $0
is set to the name of that file. If bash is
started with the -c option, then $0 is set to
the first argument after the string to be exe‐
cuted, if one is present. Otherwise, it is
set to the file name used to invoke bash, as
given by argument zero.
Portanto, $0
é o nome completo do seu script, por exemplo, /home/user/scripts/foobar.sh
. Como muitas vezes você não quer o caminho completo, mas apenas o nome do próprio script, use basename
para remover o caminho:
#!/usr/bin/env bash
echo "\#!/usr/bin/env bash
## Multiple statements on the same line, separate with ';'
for i in a b c; do echo $i; done
## The same thing on many lines => no need for ';'
for i in a b c
do
echo $i
done
is $0"
echo "basename is $(basename $0)"
$ /home/terdon/scripts/foobar.sh
$0 is /home/terdon/scripts/foobar.sh
basename is foobar.sh
O ;
só é realmente necessário no bash se você escrever várias instruções na mesma linha. Não é necessário em nenhum lugar no seu exemplo:
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, $0
is set to the name of that file. If bash is
started with the -c option, then $0 is set to
the first argument after the string to be exe‐
cuted, if one is present. Otherwise, it is
set to the file name used to invoke bash, as
given by argument zero.