2
#!/bin/bash

mainDir="$1"
fileName="$2"
preCommand="$3"

echo ${mainDir}
echo ${fileName}
echo ${preCommand}

I am trying to insert text and arguments from earlier calls into arguments for another script.

./script.sh "dir1" "filename" "text with ealier agrument ${mainDir}"

The echo from my script always returns: "text with ealier agrument"

Is it possible to do this in a script?

2
  • It appears you expect ${mainDir} to be replaced with dir1 in this example. ${mainDir} is expanded by the shell before your script is called, and it won't be expanded inside the shell. Commented Dec 16, 2015 at 19:44
  • Your description is a bit vague. Please clarify. Commented Dec 16, 2015 at 20:37

2 Answers 2

2

If you want the first argument to be part of the third, you need to to so explicitly. Either

./script.sh "dir1" "filename" "text with ealier agrument dir1"

or

first=dir1
./script.sh "$first" "filename" "text with earlier argument $first"
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the "source" command

Example:

source ./script.sh "dir1" "filename" "text with ealier agrument ${mainDir}"

When you run a script, you are spawning a new shell process as a child for your current shell (changes in child do not affect parent). When you use "source", you are running in the current shell.

Every time you run the above command, "text with earlier argument ${mainDir}", will then echo back to you what was supplied to the command previously.

1 Comment

How could source change anything relevant? The text in the argument was already substituted.

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.