Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I'm writing a simple bash script

#!/bin/bash

ls xyzzy.345 > /dev/null 2>&1
status =' echo $?'
echo "status is $status"

which finds the file xyzzy.345 and logs the result to /dev/null. But when it comes to the line status =' echo $?', I get this error:

status: Unknown job: = echo $?

But echo $? is recognized in the terminal.

Can anybody help me? My OS is ubuntu 12.04 LTS

share|improve this question
    
Have you tried the || and && syntax to check if the command was ok or failed? Have you tried a if to check if the file exists instead of checking the return value from ls? –  Johan Sep 11 '14 at 11:30

1 Answer 1

You have an extra space between status and the =. Bash variable assignments need to have no space between the name and the = sign:

status='echo $?'

What you've written is calling the status command with the single argument =' echo $?'.


All of that said, I think that what you probably want to write is:

status=$?

That stores the value of $? into the status variable.

If your real code is a little more complicated and you want to run a command and save the value, use either of these:

status=`echo "$?"`
status=$(echo "$?")

These use command substitution to access the output of a command.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.