0

Both readlink and realpath work very well in command-line.

However, when it goes within bash script, both won't work with my home folder files, like as, "~/.bashrc", "~/.config", "~/.local/bin".....

#!/bin/bash

while read i; do
    printf '%s\n' "Hi! <$i>";
    full_path=$(readlink -fv $i);
    echo $full_path;
done

Output:

Hi! <~/.bashrc>
readlink: '~/.bashrc': No such file or directory

Hi! <~/.bash_aliases>
readlink: '~/.bash_aliases': No such file or directory

Hi! <~/.config>
readlink: '~/.config': No such file or directory

Hi! <~/.emacs>
readlink: '~/.emacs': No such file or directory

Hi! <~/.emacs.d/themes>
readlink: '~/.emacs.d/themes': No such file or directory

Hi! <~/.imwheelrc>
readlink: '~/.imwheelrc': No such file or directory

Hi! <~/.local/bin>
readlink: '~/.local/bin': No such file or directory

Hi! <~/.local/share/background-images>
readlink: '~/.local/share/background-images': No such file or directory

Hi! </etc/udev/hwdb.d/10-my-modifiers.hwdb>
/etc/udev/hwdb.d/10-my-modifiers.hwdb
Hi! </etc/default/grub>
/etc/default/grub
Hi! </etc/mkinitcpio.conf>
/etc/mkinitcpio.conf

Source file contents

~/.bashrc
~/.bash_aliases
~/.config
~/.emacs
~/.emacs.d/themes
~/.imwheelrc
~/.local/bin
~/.local/share/background-images
/etc/udev/hwdb.d/10-my-modifiers.hwdb
/etc/default/grub
/etc/mkinitcpio.conf
ubian
  • 1
  • 1
    `~` is a shortcut that the shell expands to the actual actual path to your home directory, but only in certain situations. You're not using it in any of those situations, so it's actually looking for a directory literally named "~" inside the current directory, and it doesn't exist. BTW, I'm pretty sure this is an at-least-partly duplicate of earlier questions, but I can't seem to find any dupes right now. – Gordon Davisson Aug 13 '20 at 05:57
  • I found a pretty near duplicate question: [cd to a folder name starting with tilde (~) in bash script](https://stackoverflow.com/questions/25594495/cd-to-a-folder-name-starting-with-tilde-in-bash-script) – Gordon Davisson Aug 13 '20 at 06:04
  • Thank you, Gordon. I appreciate this! I'd better remove this post, hadn't I? – ubian Aug 13 '20 at 06:17
  • @ubian I'll mark it as a duplicate instead; since I had trouble finding a question about this, someone else with a similar problem will probably have trouble finding it as well. With this one pointing to the other questions, they've got another path to find answers. – Gordon Davisson Aug 13 '20 at 06:22
  • Another note, though: some of those other questions have answers recommending `eval`. Don't use it. `eval` will expand (and potentially act on) `~` *and any other bits of shell syntax in the string*. Including potentially executing parts of your filename as commands, redirects, or ... anything else you can do with shell syntax. This makes for a mess of scary potential bugs. – Gordon Davisson Aug 13 '20 at 06:26
  • @Gordon I got it. Thanks again~ – ubian Aug 13 '20 at 06:45

0 Answers0