1

I am a beginner in writing shell script (probably my 1st shell script). I have a shell script that I like to run. The command is a simple grep command based on the parameter passed to it. I managed to get the correct grep command using the script below.

FILE='testurls'
MERCHANT_ID='8'
printf "grep '^%s' %s\n" "$MERCHANT_ID" "$FILE"

./hello.sh 
grep '^8' testurls

How would I execute the command instead of printing it.

1
  • 1
    Aside: By convention, environment variables (PATH, EDITOR, SHELL, ...) and internal shell variables (BASH_VERSION, RANDOM, ...) are fully capitalized. All other variable names should be lowercase. Since variable names are case-sensitive, this convention avoids accidentally overriding environmental and internal variables. Commented Feb 22, 2016 at 10:52

2 Answers 2

2

printf is not necessary

FILE='testurls'
MERCHANT_ID='8'
grep "^$MERCHANT_ID" "$FILE"
Sign up to request clarification or add additional context in comments.

Comments

2

You can use shell backticks quotes. This will store the output of executable commands.

Syntax are :-

1) Legacy Bourne shell backticks ``:

var=`grep "^$MERCHANT_ID" "$FILE"` 
printf "%s\n" "$var"

2) $() syntax:

var=$(grep "^$MERCHANT_ID" "$FILE")
printf "%s\n" "$var"

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.