2

I need to read content of the ssh key using the given path resides in a file like following: my ssh private key is: ~/.ssh/firstname.lastname

When I call cat ~/.ssh/firstname.lastname in terminal on ubuntu, I get the expected result. However, if I would get the path to my ssh key from any text file, then I get cat: '~/.ssh/firstname.lastname': No such file or directory.

For instance, I have path_to_key.txt file containing single line ~/.ssh/firstname.lastname.

So calling cat $(cat path_to_key.txt) in terminal returns me error message mentioned above.

All files reside under the same directory and callings happen also from that directory.

Why I can not "cat" in such a way ?

2

The cats all worked, but the tilde (~) was not processed like it normally would be on a command-line. The inner cat returned the contents of the file, but the outer cat had already been processed for tilde expansions, wildcards, variable expansions, etc. Therefore, the outer cat looked for a file by the literal name of ~/.ssh/firstname.lastname instead of the intended /path/to/your/home/.ssh/firstname.lastname.

Two options for resolution:

  1. Include the absolute path to the file: instead of path_to_key.txt containing ~/.ssh/firstname.lastname, it would contain (for example) /home/you/.ssh/firstname.lastname
  2. Wrap the outer cat with eval so that the resulting command (after the command substitution occurs) is reprocessed: eval cat $(cat path_to_key.txt). Note that this bring an extra layer of evaluation, so any redirections or subsequent commands inside that key file are executed -- along with your desired tilde expansion, of course.
  • thank you very much Jeff for the quickest and perfect answer! Used the option#2 and worked fine! – Seymur Farziyev Apr 26 at 15: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.