Question:
How do I get the directory path where the bash script is located, from within the script itself?
#!/bin/bash
MINHADIR="caminho/para/onde/estou" # apanhar a diretoria onde estou atualmente
Answer:
I don't know if this answers the question but:
#!/bin/bash
echo "A script está em: $0"
echo "O invocador está em $PWD"
Update 1 print the absolute path of the script
To get the absolute path, we start by joining the current directory with the script directory ( $PWD "=" dirname $0 "/"
).
Next we rewrite (in this case using perl) the cases where q script directory is a relative path ( ./d
../../b
b/c
) using substitutions.
Consequently:
#!/bin/bash
printf "%s=%s/" $PWD `dirname $0` | # formatar como "$PWD=$0/"
perl -pe 'while(s!/[^/]+=\.\./!=!){}; # a/b=../c --> a=c
s!.*=/|=\./?!=! ; # a/b=/c --> =c ; b=./c => b=c
s!=!/!; '
or same
#!/bin/bash
script_dir=$(printf .......e mais as outras 3 linhas... )
echo $script_dir