I am using Fedora Linux. I am trying to run a docker image and this error came up. Can someone help on how to fix it? (Resetting my bashrc file didn't help nor did other quick fixes suggested here).
bash -version
GNU bash, version 5.1.16(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2020 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 gives no output:
$ exec "$#"
Try running it through #!/usr/bin/bash instead of #!/usr/bin/env bash. You can see the differences here.
Related
How do I iterate over a range of numbers in Bash when the range is given.
my code is like
for i in {1..5}
do
echo "Welcome $i times"
done
And I am expecting
Welcome 1 times
Welcome 2 times
...
However I got something like
Welcome {1..5} times
my bash version is
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
Script is missing the first line:
#!/bin/bash
Result is that a different shell may be running the script. In particular the error will occur if the script is run by sh. This is because {1..5} is a bash sequence expression and is not supported by some other shells like sh.
i have simple bash loop which is working fine in linux and not in local Git Bash which based on cygwin in windows
i have this for loop :
#!/bin/bash
for (i=2; i<4; ++i); do
echo "dddd"
done
in Git Bash it gives me this error :
./test.sh: line 5: syntax error near unexpected token `newline'
./test.sh: line 5: ` done
linux version of bash
Amazon Linux version 20xx.0x is available.
[user1 ~]$ bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
Git bash version
GNU bash, version 4.3.46(2)-release (i686-pc-msys)
Copyright (C) 2013 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.
Need TWO parentheses:
#!/bin/bash
for ((i=2; i<4; ++i)); do
echo "dddd"
done
Change your line endings from CRLF to LF. There are many ways to do this. Your favourite editor may have an option to change line endings, and also to preserve the existing line endings when saving files.
The simplest method though, is to use dos2unix. To convert in-place, use dos2unix bashtest.sh.
I've written a sh script in one of my ubuntu VMs which works fine, but when I try to run it in my other VMs, it does not work. Both VMs should be the same. With bash --version both VMs reply with:
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 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.
With lsb_release -a, both also reply:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.3 LTS
Release: 14.04
Codename: trusty
My security_steps.sh script looks like this:
#!/bin/sh
set -e
if ! [ -f svn_up_action.sh ]; then
echo "svn_up_action.sh is missing. Please make sure it is in the same directory as this script."
exit
fi
When I do: sudo sh security_steps.sh, the console errors with:
: not foundeps.sh: 6: security_steps.sh:
security_steps.sh: 7: set: Illegal option -
How can I figure out what's going on with the VM of the non-working shell? I feel like the shells are somehow different. I appreciate your help!
This almost certainly means your file has DOS newlines -- thus, hidden CR characters at the end.
Thus, set -e becomes set -e$'\r' (using bash-specific syntax to represent the CR character), which isn't a valid option.
This also explains the : not found, as a CR will reset the cursor to the beginning of the line, truncating an error message of the form sh: commandname: not found by making the commandname instead an operation that moves the cursor to the beginning of the line.
I had *.sh file throwing me this while running it on Windows (via GitBash).
I've had to change line endings to UNIX/OSX format.
In Windows:
choco install dos2unix
dos2unix file.sh
In Linux:
sudo apt install dos2unix
dos2unix file.sh
dos2unix converts the file to the Unix type.
After doing this, try to run the application again or the file.
If you run into this problem because you are touching your files on a Windows machine, Notepad++ is your friend.
Inspection
View > Show Symbol > Show All Characters
Look for CR LF line endings, rather than LF line endings!
Solving
Search > Replace (Ctrl+H)
Make sure Search Mode is set to Extended
Find \r\n and replace with \n
I got the same Error,
Later I found that these raise because My shell script file moved from a windows machine to Linux machine(there may be a chance of formating or Encoding error).
So I have created the document in Linux machine itself. It solved the issue.
This "May" help few, I am posting this because it worked for me.
You can use the below command, it will remove the hidden CR characters
sed -i 's/\r$//' filename
I'm working on a Bash library and want to ensure I'm supporting as many environments as possible - including old installations of Bash. My development environment is Bash 4.3, but some of my users may well be running much older versions and presently I have no way to confirm or deny that my library will work for them. In particular I'd like to be compatible with OSX (which still ships with Bash 3.2, AFAIK).
I know Bash can run in POSIX-compliant mode; is there a similar setting to disable modern functionality? Or a way to run Bash in some sort of compatibility mode? I'm looking for any technique short of actually finding and booting up old operating systems and testing my library there.
Update
For example, I've avoided using associative arrays since they were introduced in Bash 4, but it's hard to be sure without testing that I'm not accidentally using some other Bash 4+ feature.
Finally coming back to this question, it's pretty easy to just compile (without installing) the bash version(s) you're interested in. Here's how I'm testing Bash 3.2.57:
$ mkdir ~/bash
$ cd ~/bash
$ wget http://ftp.gnu.org/gnu/bash/bash-3.2.57.tar.gz
$ tar xvzf bash-3.2.57.tar.gz
$ cd bash-3.2.57
$ ./configure
$ make
# if `make` fails due to yacc, run `sudo apt-get install byacc`
# No need to run `make install`
$ ./bash -version
GNU bash, version 3.2.57(1)-release (armv7l-unknown-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
Now you have a bash 3.2.57 binary you can run, without actually "installing" it or modifying your normal environment.
To run a shell script against this version:
$ ./bash your_script.sh
To enter a clean interactive prompt:
$ env -i PATH="$PWD:$PATH" ./bash --noprofile --norc
bash-3.2$ bash -version
GNU bash, version 3.2.57(1)-release (armv7l-unknown-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
bash-3.2$
Using env -i rather than just calling ./bash directly leaves you with a mostly-empty environment (run env from inside the shell to see what's still set). Updating the PATH allows calls to bash (e.g. bash -version) to invoke the local bash shell, not the system-wide installation (but note this pulls in your whole PATH). Adding --noprofile --norc avoids loading your .bashrc and associated scripts.
If you don't want to pick up any PATH modifications, just execute export PATH="$PWD:$PATH" once inside the subshell instead of as part of the env command.
I have a Docker image (repo) using these installation steps, if that's helpful for folks to reference. I wouldn't necessarily suggest using this image directly, but you're welcome to copy from the Dockerfile/install script. MIT licensed.
Although it's nice to know that it's possible to compile arbitrary versions of bash locally (as discussed in my other answer), these days there's a much simpler option - the official Docker bash images.
To test a script against multiple bash versions is often as simple as:
for v in 3 4 5; do # or whatever versions you're interested in
docker run -v "$PWD:/mnt" "bash:$v" \
bash /mnt/your_script.sh
done
Have a look at shenv: https://github.com/shenv/shenv. Just like rbenv, pyenv, goenv and others, but for shells, it lets you install different versions of Bash among others (zsh, fish, yash, etc.).
(Disclaimer: I'm the one who forked pyenv into shenv!)
You can use Bash own facilities to emulate older version of Bash. Look up "compat" options on shopt.
Mind you, while it does change behavior as described under each compatNN entry in the man page, it does not remove features that are otherwise present on the current version. For example, this doesn't cause any errors:
shopt -s compat31
shopt -s globstar
Even though globstar was only introduced on Bash 4.0.
so I have this bash code in my .bashrc file...I'm using Mac OS X Yosemite
command_not_found_handle ()
{
runcnf=1;
retval=127;
[ ! -S /var/run/dbus/system_bus_socket ] && runcnf=0;
[ ! -x /usr/libexec/packagekitd ] && runcnf=0;
if [ $runcnf -eq 1 ]; then
/usr/libexec/pk-command-not-found $#;
retval=$?;
else
echo "lalalla";
retval=1;
fi;
return $retval
}
I've used this code before in linux environment and it works just fine...whenever the user enters a bash command that doesn't exist, it would default by echoing "lalalal".....however when I use this on my Mac OS X Yosemite, it doesn't work despite the fact that my .bashrc file is properly registered (I've executed source ~/.bash_profile and my .bash_profile contain the code source ~/.bashrc
other command that exist in my .bashrc are executing just fine....
what did I do wrong and how can I make mac use command_not_found_handle whenever the user enters a command that doesn't exist in mac?
UPDATE
#tristan so I upgraded bash via brew which is confirmed when I run bash --version which outputs
GNU bash, version 4.3.30(1)-release (x86_64-apple-darwin14.0.0)
Copyright (C) 2013 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.
moreover I redid source .bash_profile
nonetheless when I enter a command that doesnt exist it still displays
-bash: askldfasf: command not found
what did I do wrong?
command_not_found_handle was introduced to Bash version 4.
OS X Yosemite ships with bash-3.2 by default.
To introduce some facsimile of this, you could check the exit code of every command to look for exit code 127 ("command not found") and append a call to that function after. You could probably patch in this behavior on your own by checking the exit code from the prior command is 127 and calling your custom command_not_found_handle function (using $? and PROMPT_COMMAND).
That said, if I was in your shoes, I'd probably upgrade Bash by installing it via homebrew (brew install bash or from source.
e.g.
bash-4.3$ cat not_found.bash
command_not_found_handle()
{
echo ":("
}
bash-4.3$ source not_found.bash
bash-4.3$ a
:(
I tested your script as it currently stands on my machine as well:
bash-4.3$ source pillar.bash
bash-4.3$ a
lalalla
Test source-ing your script interactively to make sure it's the same on your machine.