All Questions
Tagged with bash-functions or bash
27,278 questions
12
votes
2
answers
1k
views
Why arithmetic syntax error in bash causes exit from the function?
I was debugging one script, and discovered unexpected behavour of handling arithmetic syntax error.
Normally, when error happens, script is just continuing execution.
#!/bin/bash
func2 () {
echo &...
4
votes
0
answers
237
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
...
2
votes
1
answer
87
views
Make `set -o vi` vim cursor reflect editing mode when in `read`
I have a script read.sh:
read -ei "hello world"
eval $REPLY
Upon executing chmod +x read.sh and then running ./read.sh, I notice that pressing Esc followed by b or w jumps between the ...
0
votes
0
answers
40
views
Is there anything like `tail -f` but that can follow multiple files and show which file each line came from? [duplicate]
I'm trying to monitor multiple log files simultaneously, but when I use:
tail -f /var/log/app1.log /var/log/app2.log
I get mixed output and can't easily tell which file each line originated from. What ...
0
votes
0
answers
37
views
Bash replacement with new line [duplicate]
The command echo ${PATH//:/$'\n'} should replace colons with new lines. However, I am getting spaces. What am I doing wrong here?
2
votes
2
answers
357
views
How to add a prefix to all functions defined in a sourced shell script
I have a Bash script my.sh with commands such as:
jup () {
conda activate
cd
jupyter notebook
}
I source this file in .bash_profile with source my.sh and then I can run the command with ...
1
vote
2
answers
132
views
How to have bash logging ("set -x") only log the main command line (not split into pipelines)?
Consider this one-liner bash example:
$ (function test() { echo "testing a string" | grep "a" | sed 's/i/y/g'; }; set -x; test | grep "test" )
+ grep --color=auto test
+ ...
2
votes
3
answers
599
views
append iterator value to variable in for loop
I have a bash variable as such
HAP="$(echo ${d} | cut -f 7 -d '/' | sed 's/[A-Z,0-9]\+//' | sed 's/_//')"
that when is run as part of a script return either ref or hap. Now, I have another ...
0
votes
1
answer
75
views
command line access to bash directory stack [duplicate]
In the tcsh shell, the elements of the directory stack can be access on the command line using =<#>, e,g, =0 =1 .... and a sampled command like:
cp Something =1/
would copy file Something to ...
0
votes
2
answers
173
views
How to prevent side effects when using an alias (e.g. cat aliased to batcat)?
I want to alias cat to batcat. Yet I do not want to break any script on my system that may depend on cat.
How to ensure that it won't break existing scripts?
I am on an Ubuntu machine on a bash ...
7
votes
1
answer
1k
views
Mysterious prompt for sudo password while opening new shell
There is a remote machine which I am accessing through ssh and every time I open a new shell either via login or tmux, I get asked for a sudo password.
I have checked the .bashrc and /etc/bash.bashrc ...
2
votes
1
answer
173
views
Bash exception handling with a stack trace and cleanup code - how to implement?
When using the set -e flag in Bash, then it's difficult to get user-friendly error messages (a stack trace) and it's difficult to add cleanup code that executes before your script exits with an error ...
0
votes
2
answers
83
views
How to start vipw/vigr without my vimrc?
my vimrc seems to not play along with vipw/vigr (probably because i only allow root to place tmp/buffers/etc in a secure location) so I tried (using bash):
user@host$ EDITOR='/usr/bin/vim -u NONE' ...
3
votes
1
answer
427
views
How can I make Arch Linux show the names of packages in the bash command not found error?
On my Linux Mint systems, when I type a command that is not found but is available from a package in the Ubuntu or Mint repositories, the command not found error message includes the names of the ...
0
votes
0
answers
84
views
A bash script problem installing Apache Cloudberry - it's a generic setup script - just isn't working for me
This is a fairly bog-standard script, but my bash knowledge isn't huge, so here goes.
The script is from here - the Apache Cloudberry project - an interesting open source version of a distributed ...
3
votes
4
answers
168
views
Is it possible to silence the initial call in xtrace, when BASH_XTRACEFD is set?
#!/usr/bin/env bash
exec {BASH_XTRACEFD}>./xtrace.log
declare -p BASH_XTRACEFD
set -x
{ : "how do you hide this in ./xtrace.log?"; } 2>/dev/null # fail
# { :; } "${BASH_XTRACEFD:-...
-2
votes
1
answer
147
views
extract fields of a string separated by |
In the below shell script I want to extract the fields of a string delimited by | character. But unfortunately I'm facing issue if there exists pipe command | in the string, because of which I'm not ...
1
vote
1
answer
97
views
What's the difference in manipulating the array between echo and printf in Bash?
I'm surprised by the difference in manipulating the array between echo and printf in Bash:
printf cannot handle += operator
Both printf and echo cannot get results of += out of while loop
So, why?
I'...
4
votes
2
answers
361
views
Deferred variable expansion including in a subshell
I have a command that takes always the same initial parameters. I can create a variable to capture these parameters. Also, I need to pass the output of a subshell running the same command to the ...
0
votes
0
answers
103
views
How to use bash history on a mac?
I've got MacBook from work and try to make it work similar to Linux. This is my config for history in bash:
# When the shell exits, append to the history file instead of overwriting it
export ...
1
vote
2
answers
373
views
get process id of a process running in remote machine
In the below shell script, I am trying to get the process id of a remote process using ps command, but not getting the required output
#!/bin/bash
get_process_id() {
local res
local ...
0
votes
1
answer
98
views
bash function returns 0 when it seemingly should return 1 or 2
bash 4.4.20(1)-release. (No portability required.)
This function works great when I pass it a valid DB name:
Get_ValidDB()
{
local pDB=$1
[ -z $pDB ] && { printf '||Requires a DB name.'...
-3
votes
3
answers
160
views
How to pass an array as an argument to a function
In the below shell script I am passing an integer and a string array to a print function which should print each parameter separately, but I'm not able to. I don't want make a reference of passed ...
-2
votes
3
answers
189
views
Generate hashes automatically and continuously with a script and stop by pressing a key?
I need a part for a script in bash where hashes are generated automaticaly and continuously and when I press a key the generation stops and the last hash at which I stopped is displayed.
I've cobbled ...
1
vote
2
answers
155
views
How to pass an argument by reference to a function in KornShell (PDKSH 5.2.14)
In bash I can use
local -n ref_name=$1
to pass an argument to a function by reference, but the same syntax in ksh throws an error saying
typeset: -n: unknown option
I tried nameref ref_name=$1 ...
1
vote
2
answers
100
views
How to pretty print one column of piped input?
bash 4.4.20 and jq-1.6 on RHEL8
I get this nice output from jq and column. Hard to read, though.
pgbackrest info --output=json \
| jq -r '.[] | .backup[] | "\(.type) \(.label) \(.info.delta)...
1
vote
2
answers
136
views
How can I temporarily bypass an alias of a keyword in Bash?
If I alias a keyword, I can't figure out how to bypass it.
$ alias if='echo "GOTCHA!"; if'
$ if true; then echo y; fi
GOTCHA!
y
The usual tools for bypassing an alias, like escaping, ...
1
vote
2
answers
268
views
Check if a given function name exists in a sourced shell script file
In Bash I can use $(type -t function_name) = "function" to check if the function by name function_name exists or not in a sourced file which is sourced using source file_name command. I'm ...
0
votes
0
answers
47
views
NetworkManager error in raspberry using Dispatcher.d events
Our goal was to create an AP mode on a Raspberry Pi easily, so we could connect to it from outside, connect to a webserver, send form details about the WiFi network, and then connect to that WiFi ...
1
vote
1
answer
86
views
List all the directories residing in a directory path using KornShell
I have the below shell script which works fine in Bash to list all the directories residing inside a given directory path and add them to an array. But the same script code doesn't work in KornShell.
...
0
votes
0
answers
35
views
Why does `<command> | tee /dev/tty | :` (where `:` is the "no effect" or "does nothing" bash builtin) display nothing? [duplicate]
I wanted to use tee /dev/tty to look into the stream passing through a pipeline. While making some tests, I observed the following:
~$ cd /proc/self/fd
/proc/self/fd$ ls
0 1 2 255
# Ok
/proc/self/...
2
votes
1
answer
264
views
bc inconsistent trailing zero result as part of a bash script variable
Consider:
heightScaled=$(bc <<< $H*$S/1)
echo $heightScaled
45.0
Why am I getting 45.0 despite the same operations yielding 45 if used outside the script either interactively or piped into ...
3
votes
1
answer
459
views
Understanding file descriptors and pipes resulting from `cat <(<command>)`
I don't understand the following:
$ cat <(sleep 60) & jobs -l
[1] 63813
[1]+ 63813 Running cat <(sleep 60) &
/proc/63799/fd$ ps --forest
PID TTY TIME CMD
...
3
votes
7
answers
1k
views
How to list only the last name.partXXX.rar file?
In Linux in a folder, without subfolders, there are many files like this scheme.
I list them with ls -1.
1yBWVnZCx8CoPrGIG.part01.rar
1yBWVnZCx8CoPrGIG.part02.rar
1yBWVnZCx8CoPrGIG.part03.rar
...
5
votes
1
answer
309
views
Differences between bash and fish read command
In bash, the following command
printf 'foo bar\n' | { read a b; printf '<%s> <%s>\n' $a $b; }
produces the output
<foo> <bar>
In fish, the following command
printf 'foo ...
4
votes
4
answers
878
views
How to check if a path exists from a string with case insensitive?
Hello i try to check if a path exists with a if statetment in a bash script from a string. I try the follow but nothing seems to handle this.
The path is /home/orhan/Blog/Mai1
path='/home/orhan/blog/...
0
votes
7
answers
2k
views
Is it possible to protect a bash script against a hostile environment?
Bash allows to export read-only variables and functions to the environment.
Also, when a bash script is run, Bash sources the file BASH_ENV was set to, unless invoked with -p.
How do you protect ...
2
votes
2
answers
397
views
Can I execute multiple case blocks if the pattern matches?
I want to know if there is another alternative in Bash to execute multiple blocks when the pattern matches.
I don’t want to use another command, if statements, or two or more separate case blocks or ...
0
votes
1
answer
65
views
Detecting outbound mail
My VMs IPv6 address occasionally get on a blocklist. Their "evidence" is that they claim something is making outbound connections to port 25 and issuing an EHLO of an IP address, not a ...
3
votes
6
answers
252
views
regex to find text plus trailing 10+ spaces; sum of two matches' lengths is part of matching condition
I'm trying to clean files that are copy/pasted versions of my Cygwin (mintty) terminal running bash1. Usually, the input and output are separated by a linefeed ('\n'), as expected. However, when I ...
4
votes
4
answers
577
views
How to wait for background commands that were executed within a subshell?
Given this code:
#!/bin/bash
set -euo pipefail
function someFn() {
local input_string="$1"
echo "$input_string start"
sleep 3
echo "$input_string end"
}
function ...
10
votes
7
answers
1k
views
Check if multiple files exist on a remote server
I am writing a script that locates a special type of file on my system and I want to check if those files are also present on a remote machine.
So to test a single file I use:
ssh -T user@host [[ -f /...
4
votes
1
answer
260
views
Why SIGTSTP (^Z/Ctrl-Z/suspend) doesn't work when process blocked redirecting write into a FIFO/pipe not open for reading yet? And what to do then?
Sometimes, a command is stalled attempting to write to a FIFO/pipe that no other process is currently reading from, but typing Ctrl-Z to put the process to the background by sending it the SIGTSTP ...
2
votes
6
answers
712
views
Awk prints only first word on the field column
Please see my linux bash script below. I can't achieve my target output with my current code. It keeps reading the whole column 4.
input_file.txt:
REV NUM |SVN PATH | FILE NAME |DOWNLOAD ...
-4
votes
2
answers
205
views
Why is arch linux bash better than Ubuntu bash?
Both of them use bash, but the one used on arch linux has way better auto-completion. But why is that? Both of them claim to use normal Bash, so how can that be true?
I forgot to clarify that what I ...
1
vote
1
answer
417
views
Shell script with getopt for short and long options -handling option with and without argument
Wanted to run a script
-- with option
./myscript.sh -a -g test
-- without option
./myscript.sh -a -g
The script looks like in below snippet, but don't see below script working correctly.
The getopt ...
2
votes
2
answers
428
views
How to test whether a secondary inet address exists on an eth interface?
RHEL9
Sometimes server 1 has the secondary address 10.143.170.80/24, and sometimes server 2 has that secondary address. My script needs to test which server has that secondary address.
However, ip ...
-2
votes
2
answers
113
views
HISTTIMEFORMAT not working as desired in RHEL 8 bash 4.4.20
so trying to capture history with date and readable timestamps AND the command should appear on same line. following is failing:
Bash ver is: GNU bash, version 4.4.20(1)-release (x86_64-redhat-linux-...
1
vote
0
answers
64
views
PROMPT_COMMAND usage in Bash on macOS
I'm trying to use Linux bash config on a mac. I don't use zsh only bash on my mac. I have problem with this variable:
# After each command, append to the history file and reread it
PROMPT_COMMAND=&...
0
votes
0
answers
81
views
How does `/dev/tty` work as a "synonym"? [duplicate]
Context
To restore the standard input, standard output and/or standard error (file descriptors 0, 1 and/or 2 respectively) of a process to the keyboard and/or display of an interactive shell (bash) ...