I want to write a bash script that accepts, as an input argument, a file specification that includes one or more uses of the special characters .
, ..
, and ~
. I want my script's output to echo Bash's full path interpretation of my script.
Here is script I have tried:
a=$(ls -l -o -g "${1}" | cut --delimiter=' ' --fields=1-6 --complement -)
echo "\\"${a}\\""
If I issue command: myscript ~/.local/../.profile
The result returned is: /home/OldManK/.local/../.profile
. But, I want: /home/OldManK/.profile
.
Another example: myscript ~/./.profile
returns: /home/OldManK/./.profile
, but, I want: /home/OldManK/.profile
.
Another (executed from /home/OldManK/.local
): myscript ../.profile
returns: ../.profile
, but, I want: /home/OldManK/.profile
.
Any alternate approaches, please?
ls
is a very bad idea that should be avoided unless you have 100% control over the input file names, but even then it will break when using-l
since the dates can change. See mywiki.wooledge.org/ParsingLs../
-- this is done by the operating system kernel as part of handlingopen()
and similar calls.~
is done by the calling shell before it's passed to your script as an argument. That is, when you runmyscript ~/.local/../.profile
, the script actually receives "/home/OldManK/.local/../.profile" as its argument. If you want your script to handle~
itself, things get more complicated (see this stackoverflow question).foo/../bar
resolved (tobar
if foo wasn't a symlink.) What's a "canonical path"? . The other part is shell-expansion of shell meta-characters like~
.