Why when I run sh -c [script], it won't accept any positional parameters?

StackOverflow https://stackoverflow.com/questions/23604113

  •  20-07-2023
  •  | 
  •  

문제

I am confused; I run a script, with sh -c , but if I want to pass a parameter, it will be ignored.

example:

# script.sh
param=$1
echo "parameter is: " $param

If I run it as

sh -c ./script.sh hello

I get nothing in the output

Why is this happening? How can I avoid this?

도움이 되었습니까?

해결책

This will work for you:

sh -c "./script.sh hello"

If you run it that way:

sh -c ./script.sh hello

than hello became sh's second parameter and ./script.sh is run with none parameters.

다른 팁

The -c switch accepts a single argument. The shell will be doing the parsing itself. E.g.

sh -c './script.sh hello'
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top