Bash scripting: how to handle ampersand in a string for a positional parameter?
by ziphem from LinuxQuestions.org on (#4XVEC)
Hello all!
I'm really stumped on this. I've been having fun in my .bashrc with shell functions, including making one for a command "pull." But I'm having a hard time figuring out how to handle strings that include "&". For instance:
~]$ pull file domain.tr/1em121cww
works no problem.
But
~]$ pull file domain.tr/1em121cww&s3fs12kfs
does not.
I know the easy solution is on the command line to simply write
~]$ pull file "domain.tr/1em121cww&s3fs12kfs"
but it's not so easy when you have multiple filenames (making use of positional parameter "${@:2}").
Is there some other way of handling the ampersand in the shell function? I thought maybe with sed, but this ampersand creates an issue before the shell function runs. I also don't want to process (escape, quote) every bash input with ampersands - just what I use for the "pull" command.
Thanks a ton for any help!


I'm really stumped on this. I've been having fun in my .bashrc with shell functions, including making one for a command "pull." But I'm having a hard time figuring out how to handle strings that include "&". For instance:
~]$ pull file domain.tr/1em121cww
works no problem.
But
~]$ pull file domain.tr/1em121cww&s3fs12kfs
does not.
I know the easy solution is on the command line to simply write
~]$ pull file "domain.tr/1em121cww&s3fs12kfs"
but it's not so easy when you have multiple filenames (making use of positional parameter "${@:2}").
Is there some other way of handling the ampersand in the shell function? I thought maybe with sed, but this ampersand creates an issue before the shell function runs. I also don't want to process (escape, quote) every bash input with ampersands - just what I use for the "pull" command.
Thanks a ton for any help!