This question already has answers here:
$BASH_VERSION reports old version of bash on macOS, is this a problem that should be fixed?
(4 answers)
Associative arrays: error "declare: -A: invalid option"
(10 answers)
Mac: Virtual Shell Bash Version does not Match Installed Version [duplicate]
(2 answers)
Closed 3 years ago.
I've been working with bash more and more on my Mac and have discovered that for some reason the declare -A command does not work.
myname$ declare -A
-bash: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
This answer says that support for associate arrays was added to bash in version 4.
When I run bash --version I get 5.0.11.
myname$ bash --version
GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[1]: https://unix.stackexchange.com/a/428205/379464
I ran bash --version and echo $BASH_VERSION as #chepner recommended and they give different answers.
bash --version
GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
SEGON207127:gnubin nberkowitz$ echo $BASH_VERSION
3.2.57(1)-release
I've already run brew install gnutils and added it to PATH in .bashrc.
Anything else I should try?
Related
I am using two scripts say script1 and script2. I call script2 from script1 which generates lots of text data. I am redirecting this text to a file using tee. But tee is stuck and doesn't move forward.
I cannot give the exact example as scripts are way too long to be included here. So giving a snippet of what's going on.
# script1
# Do some task
/bin/bash script2.sh 2>&1 | tee script2.log
capture=$(cat script2.log | grep "Successfully completed" | wc -l)
# Do some more work
#script2
# Execute some code
# Some python scripts
echo "Script End here"
Now when I execute script1.sh I am getting Script End here indicating second script completed successfully. But script1 is stuck at tee. When I removed tee and didn't capture output it worked out fine.
Did anyone face this before. Any idea what might be going on.
# System details
$ uname -a
Linux jetson-nx-jetpack461 4.9.253-tegra #1 SMP PREEMPT Sat Feb 19 08:58:27 PST 2022 aarch64 aarch64 aarch64 GNU/Linux
# Bash version
$ bash --version
GNU bash, version 4.4.20(1)-release (aarch64-unknown-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
From script2
When the script has a large number of lines (1000/2000/...), it is difficult to debug using set -x.
Either proceed based on comment from Barmar, or you include:
echo read1
read
command12
echo read2
read
command23
View the location where it is hanging.
If it is hanging exactly due to tee, use the following command:
echo read5
read
echo type tee
type tee
echo read6
read
Using this we can identify exactly where the script is hanging.
You can also use read1.1 read1.0 read1.-1 ... when debugging.
I wrote a simple bash alias to create a sort of daily tmp directory to work in:
alias datdir="mkdir $(date +'%m_%d_%Y')___$RANDOM"
When I call it repeatedly on the same day, I get this:
mkdir: cannot create directory ‘02_04_2022___24499’: File exists
However, when I simply run echo $RANDOM on the terminal, I get different numbers.
I'm running Git Bash on Microsoft Windows 10 Business, Version 10.0.19044 Build 19044
$ bash --version
GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
I am trying out this simple commands in WSL (Ubuntu):
$ OS="windows redhat centos ubuntu"
$ ARR_OS=($OS)
$ echo ${ARR_OS[0]}
<BLANK OUTPUT>
$ echo ${ARR_OS[1]}
windows redhat centos ubuntu
And it has a different output if the same commands executed in Linux:
$ OS="windows redhat centos ubuntu"
$ ARR_OS=($OS)
$ echo ${ARR_OS[0]}
windows
$ echo ${ARR_OS[1]}
redhat
I was expecting that WSL works very similar with Ubuntu Linux but I'm having some difficulty trying to get this to work.
Any idea why WSL output is not the same?
EDIT:
I have attached a sample run in my WSL terminal and incorporate suggestions in the comments as well
SEE:
Screenshot of WSL Terminal Output in Windows 10
Strange, I have tried the exact commands in OSX and produces the same result:
→ echo $SHELL
/bin/zsh
→ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
→ OS="windows redhat centos ubuntu"
→ ARR_OS=($OS)
→ declare -p ARR_OS
typeset -a ARR_OS=( 'windows redhat centos ubuntu' )
→ echo ${ARR_OS[0]}
<BLANK>
→ echo ${ARR_OS[1]}
windows redhat centos ubuntu
UPDATE:
I have found this excellent post that explains the array differences when in zsh and bash:
https://blog.mimacom.com/arrays-on-linux-shell/
I am running:
» make --version
GNU Make 4.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
This works, from bash:
$ echo $RANDOM
14522
This does not work:
$ make echo-random
With Makefile:
echo-random:
echo $(RANDOM)
Some questions:
Does make use a shell to run commands?
Is it possible to tell shell to use bash?
Can make somehow access bash special env variables?
You can invoke bash with the -c argument (that tells it the next argument is a command it has to run and exit):
echo-random:
#bash -c 'echo $$RANDOM'
This way, each invocation of make echo-random starts a new bash instance that runs the command echo $RANDOM and it produces the outcome you expect.
The answer by #axiac is good. This is an alternative:
SHELL = /bin/bash
random := $(shell echo $$RANDOM)
echo-random:
echo $$RANDOM
echo $(random)
Output:
» make
echo $RANDOM
18826
echo 16300
16300
See here and here
What will happen when I a script file and save it when it's still running, and will it print my needed results.
Let's test it.
Create a script test.sh:
#!/usr/bin/env bash
sleep 1
echo 'echo "executed overwritten"' >> "$0" # append to self
sleep 1
echo 'executed original'
and execute it:
$ bash --version
GNU bash, version 4.2.24(1)-release (i686-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ chmod +x test.sh
$ ./test.sh
executed original
executed overwritten
$
Notice that bash continued reading the modified file. It maintains its current position in the file (in bytes) when the file changes.
As a demonstration, the script
#!/usr/bin/env bash
sleep 1
dd if=/dev/urandom bs=1024 count=1 of="$0" &>/dev/null # overwrite self
sleep 1
echo 'executed original'
gives the output
$ ./test.sh
./test.sh: line 6: syntax error near unexpected token `$'\311\262\203''
./test.sh: line 6: `��z�eп9)�v��▒y�a��44'{�d��4\:�A����˷���&�$�����l�
#(ɲ��4��OϹI�n>��7��P�M�a��X.�S�a���V�m�~O<��{}������J��$��TOtRd��Nw�&��B�Dz�▒��-��<`�P<?N��▒rT�Jq�L����JY�*hz���M�����i�⫣��S+�����\��c���m�NKV�8|��xvX}�V����PTd䊄�9��7���|��/��X��
��0¤k��_�R���e�*���(qu:UUɭp/j��n��bŇ_�UR?3▒�▒�%Rn�|DE$8�QbaK)A�{ ��O>9��A�����lt�����g)s��O��M��#���w��|�����N��,W'
Notice that it attempted to execute the random gibberish.
(This is Ubuntu 12.04. Behavior may vary with other shells.)