-1

I got a script that prompts for the password. For that I use echo <password> | script.sh in my other script, so that the user inputs the password at the beginning and is not prompted for it anymore.

I want to know the exit status of the echo <password> | script.sh command. I tried status=echo <password> | script.sh; echo $status but it's just a newline. Also echo echo<password> | script.sh isn't working.

Alexandru Antochi
  • 1,295
  • 3
  • 18
  • 42
  • 1
    `man bash`, search for `$?` – gboffi Aug 04 '17 at 08:10
  • You might be looking for `${PIPESTATUS[0]}`. See [Get exit status of process that's piped to another](https://unix.stackexchange.com/q/14270/56041), [Pipe output and capture exit status in Bash](https://stackoverflow.com/q/1221833/608639), [How do you use PIPESTATUS, tee and /bin/sh together?](https://superuser.com/q/704460/173513), [How to get both PIPESTATUS and output in bash script](https://superuser.com/q/425774/173513), etc. – jww Aug 04 '17 at 10:21

1 Answers1

3

by default the exit status of a pipeline command is the exit status of last command of the pipe.

# returns 0 = SUCCESS
false | true ; echo $?

# returns 1 = FAILED
true | false ; echo $?

So to check the exit status in your case:

echo  'password' | script.sh
status=$?

# example
if [[ $status = 0 ]]; then
    echo "SUCCESS"
else
    echo "FAILED $status"
fi
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36