All Questions
Tagged with bash-functions or bash
2,948 questions with no upvoted or accepted answers
9
votes
1
answer
559
views
Colorize the command trace as well as the PS4 prompt
With the option set -x, each command is echoed to STDERR before execution, prefixed by the expanded $PS4, with the first character being repeated according to the call stack depth.
I want the output ...
8
votes
1
answer
2k
views
bash/gnu readline multiline command: move row up/down
How to move one editing row up (back) or down (forward) in a multi-line command in bash/readline?
Somethnig like gj/gk in Vim.
7
votes
1
answer
2k
views
Moving cursor to end of line when using history-search-backward
I recently added the following lines to my .inputrc:
"\e[A": history-search-backward
"\e[B": history-search-forward
However, I noticed when using this that my cursor does not jump ...
6
votes
0
answers
419
views
Bash completion own compgen prefix
I have a simple bash completion script that essentially invokes my (non-bash) program and set its output to COMREPLY, i.e.
COMPREPLY=( $(my-program -- "${COMP_WORDS[@]}") )
Some of the options accept ...
5
votes
0
answers
109
views
Subsequent "git clone" commands influencing each other?
I'm currently part of a migration from GitLab to Azure DevOps. As part of that I wrote a little bash script that iterates over the migrated git repos and for each performs a clone from both gitlab and ...
5
votes
1
answer
621
views
Why does bash print exit even when it does not exit - is this a bug?
exit with zero or one numeric argument like exit or exit 42 print "exit" and exit the shell.
exit with an invalid argument like exit hello prints an error message, prints "exit" ...
5
votes
0
answers
2k
views
How to have the results of an `fzf` selection enter my bash history?
FZF is a lovely tool, the main issue I have is that the commands I end up running with it don't end up in my bash history.
Let's say I do this
history | fzf
And I choose a command I ran recently, say ...
5
votes
0
answers
1k
views
Kernel module remove not permitted for some sudo users
I use a sudoers file, which (besides other items) contains following lines:
homebridge ALL=(ALL) NOPASSWD: /sbin/modprobe -r bcm2835-v4l2, /sbin/modprobe bcm2835-v4l2, /sbin/reboot
%sudo ...
5
votes
1
answer
1k
views
wget exit code with -nc
If I try downloading a file which already exists with wget using the --no-clobber option, it works as expected but the exit code is 1 which is crashing my scripts. Why is the exit code 1 and how can I ...
5
votes
1
answer
707
views
bash + how to avoiding duplicate entries in authorized_keys (ssh) in bash
Popular methods of adding an ssh public key to a remote host’s authorized_keys file include using the ssh-copy-id command, and using bash operators such as >> to append to the file.
An issue ...
5
votes
0
answers
634
views
Is there a command like `time` that monitors the network traffic of commands?
I can use the time command to measure the time some command takes:
time node -e ""
node -e "" 0.06s user 0.01s system 92% cpu 0.076 total
Is there a similar way to measure a ...
5
votes
0
answers
253
views
On MacOS, what character does `Esc+` or `+Esc` correspond to in Bash, when using iTerm2 terminal?
I've read that mapping Left Option key to Esc+ should fix many of the issues users of Bash, on MacOS, have with GNU Readline key-bindings.
However, I've never encountered Esc+ or +Esc before, so does ...
5
votes
0
answers
2k
views
Merge stdout and stderr, and create copies of each separately
My goal is best explained by example. To start, here's
a function that will produce sample output, on both stdout and
stderr:
output() {
printf '%s\n' 1 2
printf '%s\n' errA errB 1>&2
...
5
votes
2
answers
2k
views
Adding "sudo" commands to the bash history of the *target* user
Something that's bothered me for many years is that if I want to search my shell history for commands which were run as root, I have to check two different locations:
I have to check /root/....
5
votes
1
answer
1k
views
How can I properly use an American flag emoji in my bash prompt?
I want to use an American flag emoji in my bash prompt (i.e. PS1 environment variable). However, the American flag emoji causes the terminal cursor to offset an extra character to the right.
🇺🇸 is ...
5
votes
1
answer
812
views
Pasting into terminal including comments issue
This is somehow a follow-up to my last question: Pasting multiple commands into terminal stops at user input
Pasting the following to the terminal works as expected:
(
echo test1
# some comment
echo ...
5
votes
1
answer
2k
views
TOR hidden service not always accessible through cURL. Takes multiple tries
When I try to access a hidden service on TOR using cURL, for some reason I'm not getting access to the site 100% of the time. Many times it returns "curl: (7) Can't complete SOCKS5 connection to 0.0.0....
5
votes
2
answers
2k
views
Bash Script -- ffmpeg not being invoked as expected
I'm working on a bash script to convert my music library to ogg* but executing the right command is somehow eluding me. This is the line that should accomplish it:
ffmpeg -i "${file}" -f flac pipe:1 |...
5
votes
1
answer
423
views
git report moved lines
I want to track the number of additions and deletions that exclude moved lines. So if a commit has 10 additions, 5 deletions, and 3 moved lines, then I have 7 additions, 2 deletions excluding moved ...
5
votes
1
answer
2k
views
Terminal shows "__vte_prompt_command" error, unless "/etc/profile.d/vte.sh" is explicitly sourced
Every time I open a command terminal or press enter, this error message shows up:
bash: __vte_prompt_command: command not found
It does no harm, but it's very annoying.
~/.bash_profile is here:
# ....
4
votes
0
answers
232
views
Confused about SIGINT trap
I am confused about the processing of SIGINT and EXIT traps in Bash scripts.
I wrote this script to test:
#!/usr/bin/env bash
set -e
exit_trap() {
printf "In exit_trap \$?=$?\n"
exit 0
...
4
votes
0
answers
123
views
Recovering text of Unsaved document from process memory? (frozen Xed window - process still "running" but in Sleeping status)
The window of my text editor Xed froze with Unsaved documents just as I was doing 'File'->'Save as...' to save them... [How ironic.]
Since the process still exists, I am trying to recover the text ...
4
votes
0
answers
100
views
Why is `fc` output different through a pipe in a subshell?
Why does echo "$(fc -l -1)" show the previous command, but echo "$(fc -l -1 | cat)" show the current command?
$ testfc () {
> echo "$(fc -l -1)"
> echo &...
4
votes
0
answers
134
views
Is it possible to tab complete by whole word in Bash
Suppose I have filenames as follows on my Linux system:
$ ls -1
file-index-00012.txt
file-index-00345.txt
question.txt
I start typing, where "|" represents the cursor:
$ cat fil|
If I ...
4
votes
0
answers
269
views
Bash: Stderr being sent to a file (when it shouldn't be!)
The following code is on macOS as part of /etc/bashrc_Apple_Terminal, in order to save and restore the shell state across all open windows and tabs (note that this is therefore the very old bash 3.2....
4
votes
0
answers
74
views
Name of background job started from EXIT trap is wrong
The following script:
#!/bin/bash
set -m
atexit() {
sleep 1 &
jobs
}
trap atexit EXIT
echo Hello world
Produces this output:
$ bash ./foo.sh
Hello world
[1]+ Running ...
4
votes
2
answers
414
views
How can I see the change that caused watch -g to exit
I'm using watch -g to monitor the output of a command and exit as soon as it changes. The problem is that all the UI elements of watch (including the output of the command that I'm monitoring) ...
4
votes
0
answers
641
views
What is the meaning of ESC ( B ANSI escape code?
I'm working on the ANSI escape codes interpreter and I have this escape code from the top command.
[K
[7m PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND ...
4
votes
0
answers
594
views
bash [-+]O shopt_name doesn't work the way it's supposed to as per by the manual
The bash manual says:
"
[-+]O [shopt_option]
shopt_option is one of the shell options accepted by the
shopt builtin (see SHELL BUILTIN COMMANDS below). If
...
4
votes
0
answers
1k
views
What are exactly shells levels?
My understanding of shell levels and sub-shell was that:
Sub-shells are forks (but not execs) of the shell process created by ( ... ) constructs.
$SHLVL counts shell execs.
$BASH_SUBSHELL counts sub-...
4
votes
0
answers
2k
views
chmod always gives "illegal option" error
No matter what chmod command I run, I am always getting
chmod: illegal option -- -
usage: chmod [-fhv] [-R [-H | -L | -P]] [-a | +a | =a [i][# [ n]]] mode|entry file ...
chmod [-fhv] [-R [-H | -...
4
votes
1
answer
1k
views
Nested command substitution does not stop a script on a failure even if -e and shopt -s inherit_errexit are set
Suppose you have a following script sandbox.sh.
(This looks similar to Command substitution inside a function does not stop the script on a failure even if -e is set, but I believe a bit different ...
4
votes
0
answers
1k
views
How to show rsync progress when running as systemd service
I have a bash script sync_nodes.sh that I want to manage with a systemd unit sync_nodes.service.
I want the progress of of the rsync command to show up in my logs. When I run the command ...
4
votes
0
answers
659
views
Is it possible to fork the current pane in tmux and put it in a new pane?
Often I have set up some temporary environment variables/modules and I would like them to persist as I create a split in Tmux. Is it possible to fork the current running program (i.e. bash) and put ...
4
votes
0
answers
5k
views
Bash script calls python script that wants interactively input ( not only arguments )
I have a bash script bash.sh that looks like this:
#!/usr/bin/env bash
/usr/local/sbin/pythonScript.py firstArgument secondArgument
If I call the pythonScript.py from my terminal with the two command ...
4
votes
0
answers
4k
views
Will a child process be in a different process group than the parent process?
I always thought that a child process will be in the same process group as the parent process. But I have read the following from here:
if you run some_app from the shell, the shell creates a new ...
4
votes
0
answers
1k
views
How to bind to ctrl+x in bash?
I appear to be using GNU bash, version 4.4.12, though it's possible my organization has modified it from HEAD. I'm trying to bind ctrl+x:
$ bind -x '"\C-t": "echo testing"'
<I press ctrl+t>
...
4
votes
1
answer
525
views
is it possible to colors the prompt for (reverse-i-search) context menu?
I am using fedora 25 but I think this is a general behavior on unix system.
When I ctrl+r in a command-line environment I can start to type in and anything I type will get to be matched backward in ...
4
votes
0
answers
982
views
multiple substrings ctrl-r (reverse-i-search)
Is there a way to reverse search a previous command which contains given (multiple) substrings? In Ctrl+R or alternatives?
Suppose I remember editing a file whose name contains abc, and I am looking ...
4
votes
0
answers
1k
views
How to set the default color of output / things typed in Bash
When I am typing or viewing text on my terminal, the color is white. Any command after I type my prompt is white, and the default output (stdout) is white.
I looked and my terminal emulator does not ...
4
votes
0
answers
1k
views
Coloring terminal commands ("syntax highligting" on command line)
I do know I can color my prompt using escape sequences in the PS1 variable.
Now, I would like to know if there is a way to put some color on the bash commands I often use, on the command line itself.
...
4
votes
1
answer
354
views
How to explicitly set the tabname of a new gnome-terminal?
I want to explicity rename a tab in gnome-terminal on startup of the tab. I don't want to use gnome-terminal --title flag as that gets reset by my systems bashrc file after whatever else is supposed ...
4
votes
2
answers
500
views
bash/cygwin broken after installing rtool?
Usually I can solve problems on my own (and Google). But recently I am baffled by this strange hiccup.
Background:
I use a chain of scripts for file-processing (in Windows, under Cygwin. Scripts ...
4
votes
0
answers
1k
views
Bash command history stops working after starting new tmux session
My bash command history stops working after starting a new tmux session. No previous commands when typing up, down, reverse search, no output from running history.
I've confirmed that this happens ...
4
votes
0
answers
662
views
Replacing a running binary on read-only file system
I have a simple updater bash script, which replaces files by removing the incident old files and copying new files from an archive to the running embedded target root file system. This way, the script ...
4
votes
1
answer
491
views
Bash prompt - backspace deletes right aligned text (possible to stop?)
I have a really annoying problem. I have followed all of the protocol for making a bash PS1. I know about surrounding non-printable characters with \[ and \] or \001 and \002; however, I have a ...
4
votes
0
answers
365
views
Bash command not found due to stray garbage characters
What is the cause of this behavior?
$ cd ../
$ bash: $'\210\210cd': command not found
This happens sometimes (rarely) when I enter a command, there is no visible character preceding the cd, still it ...
4
votes
2
answers
612
views
Context-sensitive bash completion changes wildcard behavior
Somewhere along the way I've fallen into the habit of hitting tab twice after using a wildcard in commands like mv or rm, which by default causes bash to show the list of files that would match the ...
4
votes
1
answer
2k
views
Automating sshfs not working
I want to mount a remote directory and I want to automate this. So I write below script which not working properly.
#!/usr/bin/expect -f
spawn sshfs [email protected]:/home/user /mnt/remote
expect ...
4
votes
1
answer
2k
views
gnome "switch user" shortcut or command line
Is it possible to switch user of gnome desktop environment by single command or any shortcut key, in centos 6.0, or Can we create shortcut key?
I found "fusa", but still unable to install it.