I like to use the vi command line editor, however the bash_rc and bash_profile are owned by root. So what is did was create a script that I can run on multiple terminals to set the command line editor to vi. However when I use this script, it says that is sets vi to on, however after running the script, vi is still set to off.
I do not understand.
#!/bin/bash
check_set() {
chckifvi=$(set -o | grep "\bvi\b"| awk '{print $NF}')
}
check_set
echo "VIM command line editing is set to $chckifvi"
if [[ "$chckifvi" == "off" ]] ; then
set -o vi
check_set
echo "VIM Command line editing is set to $chckifvi"
else
echo "VIM Comamnd line editing already set to $chckifvi"
fi
casper#casperfi 1006$ ~/bin/editerSet.sh
VIM command line editing is set to off
VIM Command line editing is set to on
casper#casperfi 1007$ set -o
allexport off
braceexpand on
emacs on
errexit off
errtrace off
functrace off
hashall on
histexpand on
history on
ignoreeof off
interactive-comments on
keyword off
monitor on
noclobber off
noexec off
noglob off
nolog off
notify off
nounset off
onecmd off
physical off
pipefail off
posix off
privileged off
verbose off
vi off
xtrace off
Run . ~/bin/editorSet.sh, not ~/bin/editorSet.sh, to execute the script's commands inside the interactive shell you're already running. (In bash, but not all POSIX shells, you can use source as a synonym for .).
Otherwise, it runs in a new shell which exits when the script does, so the configuration changes do not last past the end of the script's execution.
Related
On mac OSX, I have this script:
#!/usr/local/bin/bash
echo -e "\e[41mError: Some error.\e[0m"
When I just run the echo -e ... in a console, it prints the colored text "Error: Some error."
When executed as a script sh myscript.sh, it litterally prints the flag and the escape characters: -e "\e[41mError: Some error.\e[0m".
When I add the script location to ~/.bash_profile and execute it as myscript.sh, it does work. But I need to be able execute it without adding it to my bash profile.
Edit: using printf works: printf "\e[41mError: Some error.\e[0m\n".
when you run the shell with sh it runs in posix compatibility mode (i.e. as the bourne shell does)
bash is a successor to this shell, one of the features it adds is the -e switch to echo
in posix shell you don't need the -e, the escapes will be evaluated anyway
in bash you do, so if you want to run bash do so explicitly
I've set the keymaps of shell using set -o vi but how do I exit vim mode in shell? Executing :q or anything doesn't seem to work. unset -o vi returns bad option -o
You cannot unset a keymap, you can only change it:
set -o emacs
Because emacs is the default keymap.
I have a bash instance without Readline support, i.e. this bash is invoked with the --noediting option. The reason is that this bash instance is used by another program.
The other program wants to know how this bash would complete the command line and for this it issues compgen commands in the bash shell, e.g.
compgen -o default I
This works perfectly in my bash without Readline support. Here an example: Let's say, we are in a directory with three files:
IMG_1234.JPG
about.html
index.html
The command compgen -o default I prints duly
IMG_1234.JPG
However now I want to switch to case-insensitive completion. Normally I issue in the shell
bind 'set completion-ignore-case on'
and in a bash instance with Readline support everything is as expected: compgen -o default I prints
IMG_1234.JPG
index.html
However in my bash instance without Readline support the bind command does nothing and I still get only the IMG_1234.JPG match.
So, my question is: How can I set case-insensitive completion suggestions (when using compgen) in a bash instance which is invoked without Readline support?
Test program:
#!/bin/bash
set -e
echo "### CASE-SENSITIVE"
bash --noediting <<EOF
compgen -o default I
EOF
echo
echo "### CASE-FOLDING"
bash --noediting <<EOF
bind 'set completion-ignore-case on'
compgen -o default I
EOF
Result:
### CASE-SENSITIVE
IMG_1234.JPG
### CASE-FOLDING
bash: line 1: bind: warning: line editing not enabled
IMG_1234.JPG
index.html
Bash version:
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
If your bash version is different, then consider upgrading, as I can't reproduce your issue with 4.3.11. (And you'll probably want to redirect stderr from the bind command; I left it in so you can see it's being executed).
When I run the /bin/bash process with 2 parameters -c and SomeUserInput,
where SomeUserInput is echo $TERM
The output is
xterm-256color
Is there a way I can set the value of $TERM via a command line parameter to /bin/bash so the above invokation of echo $TERM would print something else that I specify?
(Yes, I've done a lot of digging in man bash and searching elsewhere, but couldn't find the answer; although I think it's likely there.)
First of all, since you used double quotes, that prints the value of TERM in your current shell, not the bash you invoke. To do that, use /bin/bash -c 'echo $TERM'.
To set the value of TERM, you can export TERM=linux before running that command, set it only for that shell with either TERM=linux /bin/bash -c 'echo $TERM' (shell expression), or /usr/bin/env TERM=linux /bin/bash -c 'echo $TERM' (execve compatible (as for find -exec)).
Update:
As for your edit of only using command line parameters to /bin/bash, you can do that without modifying your input like this:
/bin/bash -c 'TERM=something; eval "$1"' -- 'SomeUserInput'
Well, you can either set the variable on your .bashrc file, or simply set with the bash invocation:
/bin/bash -c "TERM=something-else; echo $TERM"
I am using this:
$ uname -a
CYGWIN_NT-6.1 bassoon 1.7.15(0.260/5/3) 2012-05-09 10:25 i686 Cygwin
$ bash --version
GNU bash, version 4.1.10(4)-release (i686-pc-cygwin)
$ cat myexpr.sh
#!/bin/sh
echo "In myexpr, Before expr"
ac_optarg=`expr x--with-gnu-as : 'x[^=]*=\(.*\)'`
echo "ac_optarg=$ac_optarg"
echo "In myexpr, After expr"
$ cat myexpr2.sh
#!/bin/sh
set -e
echo "In myexpr, Before expr"
ac_optarg=`expr x--with-gnu-as : 'x[^=]*=\(.*\)'`
echo "ac_optarg=$ac_optarg"
echo "In myexpr, After expr"
The only difference between the two scripts is that myexpr2.sh uses "set -e"
$ echo $$
2880
$ ./myexpr.sh
In myexpr, Before expr
ac_optarg=
In myexpr, After expr
$ ./myexpr2.sh
In myexpr, Before expr
Expected behavior, so far.
If I do this in the parent shell (PID 2880, above):
$ set -e
$ ./myexpr.sh
The parent shell exits! That is pID 2880 above where I did the "set -e"
This is not the behavior on Linux or cygwin 1.5.12. Is this a bug in cygwin or BASH on cygwin?
This is not a bug, it's a feature of the Bash environment. This happens when you don't have the Bash shell environment variable execfail set, and/or the Shell environment variable errexit.
execfail - (is a BASHOPTS)
If set, a non-interactive shell will not exit if it cannot execute
the file specified as an argument to the exec builtin command.
An interactive shell does not exit if exec fails.
errexit - (is a SHELLOPTS)
Exit immediately if a pipeline (see Pipelines), which may consist of a
single simple command (see Simple Commands), a subshell command enclosed
in parentheses (see Command Grouping), or one of the commands executed as
part of a command list enclosed by braces (see Command Grouping) returns a
non-zero status. The shell does not exit if the command that fails is part
of the command list immediately following a while or until keyword, part
of the test in an if statement, part of any command executed in a && or ||
list except the command following the final && or ||, any command in a
pipeline but the last, or if the command’s return status is being inverted
with !. A trap on ERR, if set, is executed before the shell exits.
This option applies to the shell environment and each subshell environment
separately (see Command Execution Environment), and may cause subshells to
exit before executing all the commands in the subshell.
Different Linux versions have different defaults for these.
You can check which are enabled with:
echo "SHELLOPTS=$SHELLOPTS"
echo "BASHOPTS=$BASHOPTS"
and you can see all of them using:
set -o && echo -e "\n" && shopt -p
So, you need to enable yours with:
shopt -s execfail
If that doesn't work, you may also have to unset (off) the errexit of $SHELLOPTS with:
set -o errexit
For further info, see: The GNU Bash Manual!
PS. "set" is using reverse logic so if you wanna use the 'e' flag you have to use a "+": set +e