0

I have a script as follows:

var1=some_val1
var2=some_val2
var3=some_val3

varX=another_script.sh ${var1} ${var2} ${var3}

I get the following error:

./script.sh: line 5: some_val1: command not found

How do I get it to run properly? The script basically takes these parameters and runs a Hive query. If I put the Hive query back instead of another_script.sh blah blah blah, it works perfectly and the variable captures the value for use later on in the script just fine. I tried swapping it to make it more abstract and I am running into this issue. Please help. Thanks!

10
  • 2
    The shell is interpreting your line as being in the form of a=b command arg1 arg2, which runs command with the environment variable a set to value b. Thus, you're setting environment variable varX to the value another_script.sh while running the command named by var1 (presuming that expands to exactly one word, which isn't guaranteed unless you fix your quoting). Commented Jan 15, 2015 at 22:50
  • @CharlesDuffy: +1 for explaining why this is happening. Thank you! :) Commented Jan 15, 2015 at 22:52
  • 1
    ...now, it might be even better to track your arguments via a single array rather than a series of almost-identically-named variables, ie. vars=( some_val1 some_val2 some_val3 ); another_script.sh "${vars[@]}", but that's a different discussion. Commented Jan 15, 2015 at 22:56
  • 1
    (also, using a .sh extension is somewhat frowned on -- if you rewrote another_script in, say, Python or Ruby, you'd need to either change every caller to use the new extension or have it be named inaccurately -- and by convention, UNIX commands don't have extensions anyhow; you don't run ls.elf, after all, you just run ls. More haranguing on that topic at talisman.org/~erlkonig/documents/…). Commented Jan 15, 2015 at 22:59
  • 1
    Well, you wouldn't put a .sh in the command name -- you'd call it as another_script, not another_script.sh, but yes, it would work just as well. It's the #!/bin/sh or #!/bin/bash line that tells the operating system to treat it as a shell script (and which kind of shell script to treat it as -- the former is pure POSIX, the latter is bash, and others exist as well); the .sh extension has no functional effect at all. Commented Jan 15, 2015 at 23:17

1 Answer 1

2

Replace

varX=another_script.sh ${var1} ${var2} ${var3}

by

varX=$(another_script.sh ${var1} ${var2} ${var3})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I'll try that and let you know. Thank you :) Edit: Worked like a charm! I have other errors now, but at least, not this one! :D

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.