zsh on macOS Montery gives syntax error: invalid arithmetic operator - zsh

Regard this distilled example of the problem.
% echo 'echo "$((1.5*2))"' | zsh
3.
% echo 'echo "$((1.5*2))"' >x
% <x zsh
3.
% echo $SHELL
/bin/zsh
% chmod +x x
% ./x
./x: line 1: 1.5*2: syntax error: invalid arithmetic operator (error token is ".5*2")
I understand this error in bash.
If I change the script to just
echo "$SHELL"
it emits /bin/zsh as expected when executed. So I have no reason to expect bash behaviour here.
Someone explain why zsh is acting like sh/bash but identifying as zsh? I'm not interested in a work around (I have several), I want to understand this. Thanks.

Short answer: zsh is not executing the script; bash is.
$SHELL is the name of your login shell, not the currently executing shell. Unlike bash, which will use itself to execute a script with no shebang, zsh uses /bin/sh; from man zshmisc:
If execution fails because the file is not in executable format, and
the file is not a directory, it is assumed to be a shell script.
/bin/sh is spawned to execute it.
In your case, /bin/sh is a shell (either bash or dash, most likely) that does not support floating-point arithmetic.

Related

Using uglifyjs on M1 Pro with ZSH throws error

If I execute the following in terminal on my Macbook M1 Pro 2021 with ZSH
uglifyjs js/script.js --compress --mangle --output js/script.min.js -m reserved=['$','require','exports']
I get this error
zsh: no matches found: reserved=[$,require,exports]
But If I switch to bash then it's fine, can anyone help on this to get it fixed on ZSH?
Add quotes:
uglifyjs js/script.js --compress --mangle --output js/script.min.js \
-m "reserved=['$','require','exports']"
In both bash and zsh, square brackets are used for file globbing. The shell is attempting to find files that match the pattern reserved=[<one of these characters>]
In zsh, if there are no matches, you get an error. In bash if there are no matches, the shell silently includes the original pattern.
Which means in bash, you also want to use quotes to prevent file globbing. Otherwise, something like this can happen:
bash> echo reserved=['$','require','exports']
reserved=[$,require,exports]
bash> touch 'reserved=r'
bash> echo reserved=['$','require','exports']
reserved=r

Running a script according to shebang line

I've got a script on my computer named test.py. What I've been doing so far to run the program is type python test.py into the terminal.
Is there a command on Unix operating systems that doesn't require the user to specify the program he/she uses to run the script but that will instead run the script using whichever program the shebang line is pointing to?
For example, I'm looking for a command that would let me type some_command test.txtinto the terminal, and if the first line of test.txt is #!/usr/bin/python, the script would be interpreted as a python script, but if the first line is #!/path/to/javascript/interpreter, the the script would be interpreted as javascript.
This is the default behavior of the terminal (or just executing a file in general) all you have to do is make the script executable with
chmod u+x test.txt
Then (assuming text.txt is in your current directory) every time you type
./text.txt
It will look at the sh-bang line and use the program there to run text.txt.
If you really want to duplicate built-in functionality, try this.
#!/bin/sh
x=$1
shift
p=$(sed -n 's/^#!//p;q' "$x" | grep .) && exec $p "$#"
exec "$x" "$#"
echo "$0: $x: No can do" >&2
Maybe call it start to remind you of the similarly useful Windows command.

Echoing time string to log file on Solaris?

I am trying to echo the time a shell script executes with the following
EXECTIME=$(date)
echo "executed on: $EXECTIME" >> script.log
This was taken from a Unix tutorial but for some reason it is not working on a Solaris box with SunOS 5.10
The error I am getting is:
syntax error at line 2: `$EXECTIME=$' unexpected
Is there a difference in Unix and Solaris commands?
I am using usr/bin/bash
If you use csh, use following:
set EXECTIME=`date`
echo "executed on: $EXECTIME" >> script.log
You are not running bash but the legacy bourne shell /bin/sh.
Either replace the first line by:
EXECTIME=`date`
or set your script to use a modern shell like ksh or bash with adding this line which must be the first one of the script:
#!/bin/ksh
or
#!/bin/bash

run zsh script in specified folder

How to run zsh script in specified folder? How to specify a folder to run a script:
zsh script_name.sh
Documents said that: "-s Force shell to read commands from the standard input. If the -s flag is not present and an argument is given, the first argument is taken to be the pathname of a script to execute." but it does not work.
What is the difference between zsh -c ~/path1/ script1.sh (2 parameters) and zsh -c ~/path1/script1.sh?
You should just open a subshell, Execute the following from zsh or bash (including the parentheses):
(cd ~/path1 && source script1.sh)
Note: If your script is written for zsh, name it script1.zsh instead, since zsh syntax is not retro-compatible with old sh's.
This should work:
zsh -c "cd ~/path1 && ./script_name.sh"

Whats the difference between running a shell script as ./script.sh and sh script.sh

I have a script that looks like this
#!/bin/bash
function something() {
echo "hello world!!"
}
something | tee logfile
I have set the execute permission on this file and when I try running the file like this
$./script.sh
it runs perfectly fine, but when I run it on the command line like this
$sh script.sh
It throws up an error. Why does this happen and what are the ways in which I can fix this.
Running it as ./script.sh will make the kernel read the first line (the shebang), and then invoke bash to interpret the script. Running it as sh script.sh uses whatever shell your system defaults sh to (on Ubuntu this is Dash, which is sh-compatible, but doesn't support some of the extra features of Bash).
You can fix it by invoking it as bash script.sh, or if it's your machine you can change /bin/sh to be bash and not whatever it is currently (usually just by symlinking it - rm /bin/sh && ln -s /bin/bash /bin/sh). Or you can just use ./script.sh instead if that's already working ;)
If your shell is indeed dash and you want to modify the script to be compatible, https://wiki.ubuntu.com/DashAsBinSh has a helpful guide to the differences. In your sample it looks like you'd just have to remove the function keyword.
if your script is at your present working directory and you issue ./script.sh, the kernel will read the shebang (first line) and execute the shell interpreter that is defined. you can also call your script.sh by specifying the path of the interpreter eg
/bin/bash myscript.sh
/bin/sh myscript.sh
/bin/ksh myscript.sh etc
By the way, you can also put your shebang like this (if you don't want to specify full path)
#!/usr/bin/env sh
sh script.sh forces the script to be executed within the sh - shell.
while simply starting it from command line uses the shell-environemnt you're in.
Please post the error message for further answers.
Random though on what the error may be:
path specified in first line /bin/bash is wrong -- maybe bash is not installed?

Resources