Results 1 to 7 of 7
Hey,
I was writing a BASH shell program in xfce linux, and Ive run into a little problem.
My program name is prog and after i make the program executable ...
- 11-24-2007 #1Just Joined!
- Join Date
- Nov 2007
- Posts
- 2
bash shell programming
Hey,
I was writing a BASH shell program in xfce linux, and Ive run into a little problem.
My program name is prog and after i make the program executable with chmod, I type in the following on my command line:
prog $HOME *.
prog is my command, $HOME is to be used by the program as a string "$HOME", rather than the environment variable. Likewise for *, I want to use it as the string "*", rather than a wildcard.
The code for my program;prog; is as follows:
identifier=$1;
and to access the other variables i.e. *, I just use the shift command.
How do I quote the value of $1 in a way that the variable stored in $1 i.e. $HOME is treated as a string $HOME, rather than puting the value of my home directory into the variable identifier.
Thanks
- 11-24-2007 #2Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Either prefix the $ with a backslash (\$HOME) or enclose it in single quotes ('$HOME').
Play with echo to see the effect:
Learn to understand and use quoting properly and your scripts will be much better!Code:echo $HOME echo \$HOME echo "$HOME" echo '$HOME'
- 11-24-2007 #3Just Joined!
- Join Date
- Nov 2007
- Posts
- 2
See, Im facing this problem because of a shell scripting assignment.
Now the arguments will be passed, arbitrarily to my program.
The assignment requires me to use the value of $1 as a user id and all the other arguments i.e. $2 onwards as file names. Now my program has to go through all the files specified from $2 onwards and find the user id mentioned in $1 and some associated data with it.
My problem is as to how can I treat all argument values i.e. $1 onwards as non special. I have no control over the argument values, so I cant quote them on the command line. So in my code if I have the following line
id=$1, then how do I quote the value of $1 so that the string value of whatever is stored in $1 gets stored in id.
I found out that if I use the following command:
read 'id', the everything stored in id will be a non special i.e. $HOME will be stored as $HOME. Is there any way to pipe my command line arguments in the read command?
Thanks
- 11-28-2007 #4
Oh no. And no. This is an assignment problem. This is against the forum rules. Someone is going to come here and lock this thread. And this problem is very simple so you might be able to find the solution yourself.
- 11-28-2007 #5
I'm allowing this thread because the poster isn't asking us to do his or her homework for them, and instead asking a specific question about something they're trying to do. Carry on.
Registered Linux user #270181
TechieMoe's Tech Rants
- 11-28-2007 #6Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Sorry, I misunderstood the question...
RegardsLast edited by Franklin52; 11-28-2007 at 05:57 PM. Reason: misunderstood the question
- 11-29-2007 #7Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
The parameters are expanded when the shell invokes your script, so the script receives the expanded parameters. The only way is to quote them.
Regards


Reply With Quote
