Bash, know which board it's running on


How do I get the directory path where the bash script is located, from within the script itself?


MINHADIR="caminho/para/onde/estou" # apanhar a diretoria onde estou atualmente


I don't know if this answers the question but:

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.


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

script_dir=$(printf .......e mais as outras 3 linhas... )    
echo $script_dir
Scroll to Top