Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echoing a tilde to a file without expanding it in Bash

I need to write an argument to a file in a Bash script, so I'm doing something like this,

echo "Argument is: $1" >> file

The problem is that if there's a tilde (~) in the argument I don't want it expanded to the home directory. So if the user passed ~/bin as an argument to the script, it would get written as ~/bin and not /home/user/bin. How do I do this?

like image 331
gsingh2011 Avatar asked Sep 03 '12 01:09

gsingh2011


1 Answers

I assume your program is started as:

$ your_prog ~/foo

The argument is translated before your program is even started, so there's nothing you can do to prevent it other than educating the user to quote appropriately if the expansion is not desired:

$ your_prog "~/foo"
like image 161
Charles Duffy Avatar answered Oct 22 '22 21:10

Charles Duffy