4

let's say the scriptname is myscript. It's a symbolic link to thescript_1.91.sh

Is there a way to get the name of the link destination? An example: I want to write in a logfile

cat <<EOF

#########################
`basename $0`
`date -R` 
#########################

EOF

$0 holds the name of the file argument to the subshell, that is "myscript"

Is there a way to get the name of the link-destination here?

3

If your system has a readlink utility, this would do that.

cat <<END_MESSAGE
#########################
$( basename "$( readlink -f "$0" )" )
$( date -R ) 
#########################
END_MESSAGE

The -f option is used to resolve all symlinks in the given pathname recursively, not just the first link. See the manual for readlink on your system (man 1 readlink).

Also related:

|improve this answer|||||
  • See also: mywiki.wooledge.org/BashFAQ/029 – m0dular Jun 29 '18 at 17:05
  • Hi, readlink did it! I didn't know that command. Sometimes I also use the $() convention. But most of the time I use backticks like I did in the last ~25 years.... Thank you! – Michael Jun 30 '18 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.