bash: shift: shift count out of range
adamsmd opened this issue · comments
When ansi
is called with no arguments other than flags, it prints an error.
For example:
$ ansi --report-position
3,1
bash: shift: shift count out of range
(Was is tested with Bash version 5.1.8(1)-release (x86_64-pc-linux-gnu) on Ubuntu 20.10.)
Proposed Solution
Change
toshift 2> /dev/null || :
I can not get the error to happen. I am also using Ubuntu 20.10 with the same version of Bash. I've even written a simple script to see if I can trigger the behavior and can not.
#!/usr/bin/env bash
shift || :
set -e
shift || :
set -E
shift || :
set -u
shift || :
set -o pipefail
shift || :
I don't know what is different between our environments.
I think it would be better to not call shift
if there are no arguments, so I propose a different solution.
diff --git a/ansi b/ansi
index a8602bd..5655743 100755
--- a/ansi
+++ b/ansi
@@ -1600,9 +1600,9 @@ ansi() {
done
printf '%s' "${1-}"
- shift || :
- if [[ "$#" -gt 0 ]]; then
+ if [[ "$#" -gt 1 ]]; then
+ shift || :
printf "${IFS:0:1}%s" "${@}"
fi
This will move the shift
inside of the check to see if there's more arguments. Also note that we change the number of required arguments from 0 to 1.
Please let me know if this change eliminates the problem. Plus, I would be very interested to know your settings using two commands. Here is what I have for mine.
$ echo "$-"
himBHs
$ set +o
set +o allexport
set -o braceexpand
set -o emacs
set +o errexit
set +o errtrace
set +o functrace
set -o hashall
set -o histexpand
set -o history
set +o ignoreeof
set -o interactive-comments
set +o keyword
set -o monitor
set +o noclobber
set +o noexec
set +o noglob
set +o nolog
set +o notify
set +o nounset
set +o onecmd
set +o physical
set +o pipefail
set +o posix
set +o privileged
set +o verbose
set +o vi
set +o xtrace
$
If this works for you and the errors are removed, I'll tag this as the next release.
Thank you very much for taking the time to report and diagnose this issue. I really appreciate it.
It seems there was one more precondition to triggering this error that I hadn't realized. The call to ansi
must be a call to the ansi
function defined by your script, not a call to the script itself. So something like the following is required:
$ source ./ansi
$ ansi --report-position
5,1
bash: shift: shift count out of range
Regardless, I have confirmed that your commit has fixed the issue.
It is probably moot, but here are the shell settings you asked for.
$ echo "$-"
himBHs
$ set +o
set +o allexport
set -o braceexpand
set -o emacs
set +o errexit
set +o errtrace
set +o functrace
set -o hashall
set -o histexpand
set -o history
set -o ignoreeof
set -o interactive-comments
set +o keyword
set -o monitor
set +o noclobber
set +o noexec
set +o noglob
set +o nolog
set +o notify
set +o nounset
set +o onecmd
set +o physical
set +o pipefail
set +o posix
set +o privileged
set +o verbose
set +o vi
set +o xtrace
Very interesting, thank you for sharing your settings. I couldn't replicate it, but I am quite happy that it works for you. Tagged version 3.0.1. I appreciate your help for closing this bug.