-1

I have 4 parameters adding to excetute file:

./project01.sh /*  */  /^  ^/

and i want to make each parameters as variable but before that i want to modify it because it will be unconvenient for futher operation. So i would like to make it like "/*" so put it in "" and before each character give \ because some of then are special characters. I tried use like that:

beg1=echo $1 | sed(some change with $1)

but it change immediately $1 which is /* to direction /bin /boot itd. what can i do in that case?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
stanisz93
  • 31
  • 4

1 Answers1

1

First, when calling your script, there's nothing you can do to avoid quoting. You must call it as:

./project01.sh '/*' '*/' '/^' '^/'

...if you want to prevent any potential for shell manipulation. (^ is safe with some shells but not all).

This is because in the case of ./project01.sh /* (without quotes), expansion happens before the script is even started, so once your script is running, it's too late to make changes.


Second, use more quotes within your script:

echo "$1" | sed ...

...or, better (to fix cases where $1 contains -E, -n, or a similar value):

printf '%s\n' "$1" | sed ...

...or, better yet, if your shell is bash rather than /bin/sh...

sed ... <<<"$1"

However, if your goal in using sed is to add syntax quoting to your arguments, this will never work: The arguments are already expanded before the script is even run, so $1 is already /bin.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441