0

For example, if i have a shell program "shell1" and run "./shell1 hello world", how can I store the hello world in a variable? If I try to use read, it only accepts user input after I run ./shell1 first.

1 Answer 1

2

You are referring to parameters being passed to the script.

To capture them use $1 for the first parameter, $2 for the second, $3 for the third, and so forth. Use "$@" to capture all parameters into a single variable.

For example, try adding the following to your script:

param1="$1"
param2="$2"
echo "Param1 is $param1, Param2 is $param2"

allParams="$@"
echo "All params are: $allParams"

Take a look at Advanced Bash-Scripting Guide: Positional Parameters for more information.

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.