What does “1>&2” mean in bash? [duplicate] - bash

This question already has answers here:
What does " 2>&1 " mean?
(19 answers)
Closed 6 years ago.
What does 1>&2 mean in a bash script?
For instance, what does the following line from a bash script do?
echo "$1 is not a directory!" 1>&2
I use MacOS X. My bash script is:
if [ ! -d $1 ]; then
echo "$1 is not a directory" 1>&2
exit 1
fi

It outputs the message to stderr. Therefore 1>&2 means redirect stdout to stderr.
3. All about redirection

Related

How to parse the error given by a bash script? [duplicate]

This question already has an answer here:
Bash how do you capture stderr to a variable? [duplicate]
(1 answer)
Closed 2 years ago.
Given this script:
#!/bin/bash
COMMAND=$(echo test | jq)
if [[ $COMMAND == *"error"* ]]; then
echo failed
else
echo success
fi
Since the output of $(COMMAND) is the following: parse error: Invalid literal at line 2, column 0, I'd expect to have as output of that script failed, but I'm having success.
How can I parse the error that I got on scripts?
jq prints errors to stderr so you'd have to do this:
COMMAND=$(echo test | jq 2>&1)
But checking if command failed can be done using much easier method in
Bash:
#!/usr/bin/env bash
if ! echo test | jq '.'
then
echo failed
else
echo success
fi

Can a bash script distinguish between being called as a script and being run as a "source"? [duplicate]

This question already has answers here:
How to detect if a script is being sourced
(22 answers)
Closed 3 years ago.
I have a bash script that has inside it:
exit 1
When I "source" this script instead of running it, it causes the caller to exit.
Is there a way that the script can determine that it's being run with "source" and not as its script?
You can use this check inside your script:
[[ $0 = $BASH_SOURCE ]] && echo "normal run" || echo "sourced run"
Or using if/else/fi wherever you're calling exit:
if [[ $0 = $BASH_SOURCE ]]; then
exit 1
else
# don't call exit
echo "some error..."
fi

Automatically exit when bash command produce return code non zero [duplicate]

This question already has answers here:
Automatic exit from Bash shell script on error [duplicate]
(8 answers)
Error handling in Bash [closed]
(15 answers)
Closed 4 years ago.
In bash how do we make the script to automatically exit if a command line return code is not zero. For example:
#!/bin/bash
cd /something_something
mv file_a /somedir/file_a # this produce an error
echo $? # This produce a non-zero output
echo "We should not continue to this line"
I know we can debug bash script with #!/bin/bash -x but sometime the script is too long, it run so fast, and we missed important error.
And I don't want to keep writing
[[ $? -ne 0 ]] && run next_command
There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.
if cd /something_something && mv file_a /somedir/file_a; then
echo $?
exit
fi
echo "Both cd and mv worked"

Bash judgement gets the unexpected result [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 5 years ago.
This is the code of the my shell script:
#! /bin/bash
if ["$SHELL" = "/bin/bash"];then
echo "this is bash"
elif ["$SHELL" = "aa"];then
echo "this is aa"
else
echo "this is not /bin/bash, but $SHELL"
fi
why I execute the test_bash_03 script file gets the else result? shouldn't it be:this is bash ?
aircraftdeMacBook-Pro:bash_demo ldl$ ./test_bash_03
./test_bash_03: line 3: [/bin/bash: No such file or directory
./test_bash_03: line 5: [/bin/bash: No such file or directory
this is not /bin/bash, but /bin/bash
And I echo the $SHELL I also get the /bin/bash
aircraftdeMacBook-Pro:bash_demo ldl$ echo $SHELL
/bin/bash
You are missing a space after [ and before ].
The bash tries to execute a command named [/bin/bash instead of [ (which is test), then doesn't find that and has an exit code of 1 (false). So you end up in the else case.

What does 1>&2 mean in shell? [duplicate]

This question already has answers here:
echo that outputs to stderr
(15 answers)
What does " 2>&1 " mean?
(19 answers)
Closed 8 years ago.
Pretty noob question, what does the 1>&2 do in this script?
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
That redirects the line "This script must be run as root" from standard out (STDOUT) to standard error output (STDERR).
It's a easy way to print an error message to STDERR - this matters if you run the bash script from another script (like crontab), matters much less if you run it from the command line driectly since your terminal will show both STDOUT and STDERR.
See also echo that outputs to stderr

Resources