0

I had to use some bash code called from /bin/bash as a command followed -c option. I got something that is difficult to understand. This passed as expected:

root@punk:/home/s# function a () { echo $1 "world";}&&a hello
hello world
root@punk:/home/s# 

But this missed the parameter of the function

root@punk:/home/s# /bin/bash -c "function a () { echo $1 "world";}&&a hello"
world
root@punk:/home/s# 

How to fix the second case?

1 Answer 1

1

Since you're using double quotes, with

/bin/bash -c "function a () { echo $1 "world";} && a hello"

, the current value of $1 will be replaced which must be empty in your environment so it'll become

/bin/bash -c "function a () { echo "world";} && a hello"

which is actually

/bin/bash -c "function a () { echo world;} && a hello"

.

You can use single quotes for the outer quoting:

[STEP 101] # /bin/bash -c 'function a () { echo $1 "world";} && a hello'
hello world
[STEP 102] #
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.