bash - How do I get the directory from where I call the script when the script itself is in a different directory? -
this question has answer here:
i making few custom commands use on debian server , putting them in /usr/bin folder.
one custom command wish make involves getting directory call script, not directory in script resides.
nearly of questions find on here involve getting working directory using
a=$pwd
or
a=$(pwd)
this returns directory in script resides.
edit: aware of
$oldpwd
the above work of time.
is possible want?
current form of script:
#!/bin/bash if [ -z "$1" ] a=$(pwd) echo "unlocking current directory." sudo chmod 777 -r $a else echo "unlocking directory @ \"$1\"" sudo chmod 777 -r $1 fi
one custom command wish make involves getting directory call script,
try this
#!/bin/bash pwd
contrary you've mentioned pwd
should give place script called
sample run
user@host:~/documents/so$ ./myscript.sh /home/me/documents/so user@host:~/documents/so$ cd .. user@host:~/documents$ ./so/myscript.sh /home/me/documents
to contrary, if wish know script exists within script - no matter called - do
#!/bin/bash readlink -m "$(which $0)" #method1 readlink -m "${bash_source}" #method2 , preferred reasons mentioned @charlesduffy's comments
Comments
Post a Comment