1

I have the following shell script

#!/bin/sh
keyExists=`stat ~/.ssh/id_rsa &> /dev/null; echo $?`
echo $keyExists

When I run it as ./test.sh or sh test.sh it outputs

0 File: `/home/vagrant/.ssh/id_rsa' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fc00h/64512d Inode: 2888560 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ vagrant) Gid: ( 1000/ vagrant) Access: 2014-07-23 11:40:33.355848310 -0400 Modify: 2014-07-23 11:40:33.355848310 -0400 Change: 2014-07-23 11:40:33.355848310 -0400 Birth: -

However when I run each command individually on the command line

keyExists=`stat ~/.ssh/id_rsa &> /dev/null; echo $?`
echo $keyExists

I get the output

0

Why am I seeing this additional output and how do I suppress this additional output when I run the shell script?

2
  • 2
    Maybe using /bin/sh runs sh instead of bash, or if it's bash, turns off bash4 enhancements? &> is functional only in bash 4 and later. Commented Jul 23, 2014 at 16:12
  • 1
    &> is available in earlier versions of bash as well. Commented Jul 23, 2014 at 17:07

2 Answers 2

2

Before testing, make sure your current shell is /bin/sh, not /bin/bash.

As far as I known: &> is a new feature in bash, not in the old sh.

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of using &>/dev/null do it as >/dev/null 2>&1. This would make your script work even with ancient sh shells. You can also change your header to #!/bin/bash if you indeed have bash. Or see output of which bash to know.

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.