2

This is either dead simple or can't be done, but after hours of looking I'm stumped. Just started using bash for an Apple script.

show_name=${BASH_REMATCH[1]} #show_name is declared

test "$show_name" == "Smash" && {show_name="Smash (2012)"} #redeclare the value from 'Smash' to 'Smash (2012)

Also tried some variations e.g.

test "$show_name_temp" == "Smash" && (show_name="Smash (2012)"; exit 1)

However the value show_name never gets redeclared. The if statement is definitely working because if I put an echo "hello, world" in, it prints.

I'm coming from PHP though, so my desktop syntax is lacking.

Thanks guys

1
  • standards-compliant versions of test only accept = as an operator, not ==; the latter is a GNU extension, and using it makes your code less portable. Commented May 11, 2012 at 12:09

3 Answers 3

4

AFAIK, the first is syntax error.

The second, the round parens will introduce another context, so any variables defined there won't be visible outside it. What happens in parens stays in parens.

I believe you want this:

test "$show_name" == "Smash" && show_name="Smash (2012)"
Sign up to request clarification or add additional context in comments.

3 Comments

It's not actually a syntax error, it declares a block to run in the current shell, but assignments in the block do not propagate outside the block (apparently). (Oh well, in POSIX, it is in fact a syntax error because you need a semicolon before the closing brace.)
@triplee assignments propagate from {} blocks, but not from () subshells (which are, indeed, separate processes -- created via a fork operation -- not merely a new context within the existing shell).
@tripleee: ... and spaces around the { and }. Spaces will mess you up in bash if you're not careful. Thus, syntax error; and in this case they were superfluous.
3

Perhaps the brackets are getting in the way:

$ show_name="Smash"
$ test $show_name == "Smash" && show_name="Smash (2012)"
$ echo $show_name
Smash (2012)
$ 

Note that parenthesis starts a subshell -- variable assignments made within the parens won't be propagated to the parent shell:

$ show_name="Smash"
$ test $show_name == "Smash" && (show_name="Smash (2012)")
$ echo $show_name
Smash
$ 

Comments

1

Braces do not break a string into words, so they need to be separated from their contents by whitespace. Also, the command inside the {} must be terminated with a semi-colon (;).

test "$show_name" == "Smash" && { show_name="Smash (2012)"; }

That said, the braces are optional, as you only have one command following the && operator, so Amadan's answer is preferred, and sarnold's answer explains the problem with using parentheses. I just show how the version with braces can be made to work.

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.