2

I have a command that i'm exeuting, this command is stored in some variable, while this variable that stores this command stores another variable The thing is when i'm changing my second variable it's not dynamically changed in the my first variable, so the command that is being executed always with the same variable. Here is my code:

test="test"
TEMP_STR="doesn't exist"
checkClient=`p4 client -o -t $test 2>&1`
echo this is the output: "$checkClient"
test="${test}_2"
echo echo this is the new client name "$test"
echo this is the new output: "$checkClient"

This is the output:

this is the output: Client 'test' doesn't exist.
echo this is the new client name test_2
this is the new output: Client 'test' doesn't exist.

Any idea how to solve this ?

6
  • 1
    You never run the command a second time. Commented Feb 3, 2015 at 14:56
  • I'm using this command in the 2nd echo Commented Feb 3, 2015 at 14:57
  • 3
    checkClient=`p4 client -o -t $test 2>&1` you are NOT storing command here but executing command and storing its output. Commented Feb 3, 2015 at 14:58
  • 1
    No. You are using the output from the first command in the second echo. The backticks run the command. They don't store a command to be run through the variable. Commented Feb 3, 2015 at 14:58
  • You expect $checkClient to change every time you change $test? Commented Feb 3, 2015 at 14:59

1 Answer 1

1

Based on how you've described and then commented it seems you like to have behavior that whenever a variable changes then you want a related command to return different values based on variable's new value.

You can actually use a BASH function for this:

checkClient() { p4 client -o -t "$arg" 2>&1; }

Now use it as:

arg="test"   
echo "this is the output: $(checkClient)"

arg="${test}_2"
echo echo this is the new client name "$arg"
echo "this is the new output: $(checkClient)"
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.