Behaviour of 'ECHO' in csh and bash - bash

I was writing few scripts and suddenly stroke me to think the following commands in CSH and BASH shells.
In csh shell
$ echo $?BASH
0
$ echo $?HOME
1
$ echo $? home
0 home
In bash shell
$ echo $?BASH
0BASH
$ echo $?HOME
0HOME
$ echo $? home
0 home
Someone explain the above behaviour. I know the echo $? prints the exit status of the last command but if added with some string then how its working?

If you review the documentation csh indicates:
$?name
${?name}
Substitutes the string 1 if the variable name is set, 0 if it is not.
$?0
Substitutes 1 if the current input file name is known, 0 if it is not.
http://www.mkssoftware.com/docs/man1/csh.1.asp
be careful, $? is diferent to $?name

Related

What's the best way to preserve an exit code or assign it to a variable with Bash?

I'm trying to save the exit code from running psql in a shell script. Assuming the psql command right before this snippet was bad, I'm expecting this snippet to return anything but 1, which it initially does. But when I assign it to exitcode and then echo it, the returned value is a 0...
$ echo $?
1
$ exitcode=$?
$ echo 'simply'
simply
$ #echo $?
0
$ #echo 'coding'
coding
$ echo $exitcode
0
I'm trying to get the variable exitcode to print or echo 1 like the first line does. How do I exactly do this?
When you print $? the second time, it's not the exit code of the previous command.
You need to assign $? to exitcode immediately before running any other command.
i.e.
$ psql
$ echo $?
1
$ exitcode=$?
should be
$ psql
$ exitcode=$?
in order to preserve the exit code of psql.

How to check the current shell and change it to bash via script?

#!/bin/bash
if [ ! -f readexportfile ]; then
echo "readexportfile does not exist"
exit 0
fi
The above is part of my script. When the current shell is /bin/csh my script fails with the following error:
If: Expression Syntax
Then: Command not found
If I run bash and then run my script, it runs fine(as expected).
So the question is: If there is any way that myscript can change the current shell and then interpretate rest of the code.
PS: If i keep bash in my script, it changes the current shell and rest of the code in script doesn't get executed.
The other replies are correct, however, to answer your question, this should do the trick:
[[ $(basename $SHELL) = 'bash' ]] || exec /bin/bash
The exec builtin replaces the current shell with the given command (in this case, /bin/bash).
You can use SHEBANG(#!) to overcome your issue.
In your code you are already using she-bang but make sure it is first and foremost line.
$ cat test.sh
#!/bin/bash
if [ ! -f readexportfile ]; then
echo "readexportfile does not exist"
exit 0
else
echo "No File"
fi
$ ./test.sh
readexportfile does not exist
$ echo $SHELL
/bin/tcsh
In the above code even though I am using CSH that code executed as we mentioned shebang in the code. In case if there is no shebang then it will take the help of shell in which you are already logged in.
In you case you also check the location of bash interpreter using
$ which bash
or
$ cat /etc/shells |grep bash

why echo return value ($?) after pipeline always return "0"

I realize the fact but I don't know why:
cat abc | echo $?
if abc does not exist, but above command still return 0. Anyone knows the theory about why?
The reason why it must be this way is that a pipeline is made of processes running simultaneously. cat's exit code can't possibly be passed to echo as an argument because arguments are set when the command begins running, and echo begins running before cat has finished.
echo doesn't take input from stdin, so echo on the right side of a pipe character is always a mistake.
UPDATE:
Since it is now clear that you are asking about a real problem, not just misunderstanding what you saw, I tried it myself. I get what I think is the correct result (1) from a majority of shells I tried (dash, zsh, pdksh, posh, and bash 4.2.37) but 0 from bash 4.1.10 and ksh (Version JM 93u+ 2012-02-29).
I assume the change in bash's behavior between versions is intentional, and the 4.1.x behavior is considered a bug. You'd probably find it in the changelog if you looked hard enough. Don't know about ksh.
csh and tcsh (with $status in place of $?) also say 0, but I bet nobody cares about that.
People with bigger shell collections are invited to test:
for sh in /bin/sh /bin/ksh /bin/bash /bin/zsh ...insert more shells here...; do
echo -n "$sh "
$sh -c 'false;true|echo $?'
done
It does not have anything to do with cat abc, but with the previous command you executed. So the code you get when doing cat abc | echo $? is telling if the previous command in your history was successful or not.
From man bash:
Special Parameters
? - Expands to the exit status of the most recently executed foreground pipeline.
So when you do:
cat abc | echo $?
The echo $? refers to the previous command you used, not cat abc.
Example
$ cat a
hello
$ echo $?
0
$ cat aldsjfaslkdfj
cat: aldsjfaslkdfj: No such file or directory
$ echo $?
1
So
$ cat a
$ cat a | echo $?
0
$ cat aldsjfaslkdfj
cat: aldsjfaslkdfj: No such file or directory
$ cat a | echo $?
1
echo $? will give output of previous command which you have executed before not output of piped command. So, you will always get echo $? as 0 even if command failed before pipe.
You pipe the output from 'cat abc' to 'echo $?' which is not what you want.
You want to echo the exit code of 'cat'
cat abc; echo $?
is what you want. Or simply write it in two lines if you can.

Unbound variable not causing exit from subshell when set -eu

$ cat test.sh
set -eu
echo "`wc -l < $DNE`"
echo should not get here
$ /bin/bash test.sh
test.sh: line 2: DNE: unbound variable
should not get here
I'm running bash version 4.1.2. Is there a way to make sure all such usage of unbound variables in subshells cause the script to exit without having to modify each call involving a subshell?
The better solution to ensure variable sanitisation
#!/usr/bin/env bash
set -eu
if [[ ${1-} ]]; then
DNE=$1
else
echo "ERROR: Please enter a valid filename" 1>&2
exit 1
fi
By putting a hyphen in to the variable name inside curly braces like this allows bash to sanely handle the variable being undefined. I also highly recommend looking at the Google shell style guide, it's a great reference https://google.github.io/styleguide/shell.xml
[[ -z ${variable-} ]] \
&& echo "ERROR: Unset variable \${variable}" \
&& exit 1 \
|| echo "INFO: Using variable (${variable})"
Use a temporary variable so as to let test.sh process know about the failure of wc. You can change it to:
#!/bin/bash
set -eu
out=$(wc -l < $DNE)
echo $out
echo should not get here
Now, you won't see the should not get here if wc fails.

What does $? mean in shell scripting?

I encountered $? in one of the shell scripts I work on integrating (not written by me).
Just wanted to confirm that it means the return code of the previous command.
The usage is something like
runSomeCommand $VAR1 $VAR2 $VAR3
processResult $?
$? is the exit status of the last executed command.
ls
....
echo $?
0
$ ls notexistingfile
ls: cannot access notexistingfile: No such file or directory
echo $?
2

Resources