All Questions
Tagged with bash-functions or bash
27,278 questions
0
votes
2
answers
210
views
How can I run if statements on a command output without terminating it?
I have to run four separate if statements (and their consequences) on one command output continuously as it updates (doing four different things based on four different strings which might be in the ...
0
votes
0
answers
69
views
I want to print debug output on failure, but ERR trap doesn't fire
I want to only print the debug output on failure, this is my attempt:
#!/bin/bash
set -euo pipefail
dt-api() {
# shellcheck disable=SC2086
curl \
--fail-with-body \
--silent \
...
4
votes
4
answers
479
views
Extend multiple globbing patterns with same additional pattern at the same time
I want to extend multiple globbing patterns with the same extension in a concise way. Here's what I tried (with only two initial patterns for demonstration purposes):
myglob="*.log *.json"
...
0
votes
0
answers
21
views
Shell expension in variable allocation containing the char '*' [duplicate]
For the shell fans (bash) :
I want a variable to get a value resulting from an operation output which contains an globbing character (such as *), and the shell expansion always happens, even if I ...
0
votes
0
answers
48
views
Shell script not behaving as expected expanding variable as command [duplicate]
I am trying to script a job which interacts with mysql. I am using a variable to store a long command line. But when run via a script this does not behave as expected. I can reproduce the issue as ...
0
votes
1
answer
119
views
What is (if any) the file descriptor of /dev/tty?
The urgent issue to read keyboard input in the pipeline is solved by the answer in https://stackoverflow.com/questions/15230289/read-keyboard-input-within-a-pipelined-read-loop:
mycommand-outputpiped |...
-7
votes
2
answers
185
views
How can I pick random items from a list, but some more often than others, in a Bash config file? [closed]
In Linux, using Bash, I have two scripts:
script.sh is the main script, and
script_config.sh is the configuration for script.sh.
In script.sh, I do source script_config.sh to load all the config ...
-3
votes
2
answers
295
views
Is Bourne shell shebang #!/bin/sh expecting BASH POSIX-correct? [closed]
I've recently been playing with the legacy Bourne Shell from the GitHub repo heirloom-sh. It's installed at /usr/local/bin/sh with no symlinks. One script is invoking it via #!/bin/env sh.
I'm running ...
4
votes
1
answer
713
views
Why does tab autocomplete sometimes not work?
In bash (Ubuntu 24.10, but I guess this would apply to other versions/distributions) when you type in a console a command you can use TAB to autocomplete the filenames of the current directory. ...
0
votes
1
answer
107
views
RUNUSER && SU: pass env and run app, permissions trouble
I'm new to working with OS and unix, I'm trying to create separate users for an application, nginx, etc... and in order to run the process on their behalf. I'm currently debugging the following ways ...
-1
votes
1
answer
96
views
Added quotes on the name of the new created directory [duplicate]
I have the following situation: I'm creating a directory with an & at the end, something like this: inventory&
But, when I run the command mkdir "inventory&" the linux system ...
7
votes
2
answers
892
views
Is it possible to define a bash heredoc with a multi-word delimiter that expands variables?
Consider the following script:
#!/bin/bash
foo=Hello
bar=Word
cat <<EOF
No quotes single word delimiter expands
$foo
$bar
EOF
cat <<'EOF'
Single-quoted single word delimiter does not ...
1
vote
2
answers
89
views
Using bash history expansion, can you replace the first argument of the last time a command ran, when it's not the most recent command?
The GNU History Expansion reference lacks examples, so I can't tell if this is possible.
Here's what I want to do. I ran cat long/path/to/file1.txt | less, then I ran vim file2.txt. Using history ...
1
vote
2
answers
119
views
Why are some terminal control characters shadowed by readline
stty -a shows cchars: stop = ^S, reprint = ^R.
Ctrl-r is shadowed by readline's reverse-search-history, so when I press it I get the readline function
Ctrl-s is not shadowed by readline's forward-...
2
votes
2
answers
445
views
Bash script: ffmpeg problems with yt-dlp [duplicate]
I am trying to write a bash script to download all the videos from a given public YouTube playlist. Here's what I have so far:
#!/bin/bash
# Default values
decode_video=true
output_format="avi&...
0
votes
2
answers
135
views
How to analyse a number of different datasets with a script and save the output for each dataset in a different new directory?
I am interested in analysing 50 different datasets with a script and saving the output of each of the 50 analyses in 50 different corresponding directories. To avoid doing this manually, how could ...
0
votes
1
answer
68
views
bind -x with unicode codepoints
I instructed my terminal emulator to send "\u{85}" (C1 control character "NEXT LINE" (NEL)) down the pipe to be caught by bash:
bind -x '"\u0085":"echo Hello"'
...
-3
votes
2
answers
184
views
Add a character to duplicate emails using bash only
Input data:
id,location_id,name,title,[email protected],department
1,1,Susan houston,Director of Services,[email protected],
2,1,Christina Gonzalez,Director,[email protected],
3,2,Brenda brown,"...
5
votes
2
answers
715
views
The shell not redirecting output of tar to file
I am assuming this is a simple issue, but I don't have any one to double check my work.
Here's my bash script
#!/bin/bash
# establish date format and dump name
DATE=$(date +"%Y%m%d-%H%M")
...
0
votes
2
answers
71
views
Why are control characters added to test.md?
I am using a dialog in order to modify a single value into a file:
#!/usr/bin/env bash
prompt_and_save() {
local file=$1
local message=$2
local value=""
# Check if file ...
-1
votes
2
answers
108
views
extract multiple words from git log
I need to extract multiple words from git log.
I need the commit id and ticket number for each commit, I then need to run this through a loop and do some further processing for each commit.
commit ...
0
votes
0
answers
32
views
Is there a way to keep bash for reformatting my history? [duplicate]
I commonly want to do something with a bunch of files, so I type in something like:
$ for nam in *.c
> do
> echo $nam
> done
Aaah, I failed to use upper case C so I go to edit again and ...
1
vote
1
answer
127
views
Why is the logic handled differently in rsync '--delete src/*', '--delete src/' and '--delete src/.'
I want to copy and synchronize directories using rsync with --delete or --delete-excluded in a bash script or shell on a Debian system.
The directory was not synchronized at first, meaning the deleted ...
0
votes
2
answers
173
views
How to "catch" the output of a command? [duplicate]
I'm struggling with the vocabulary here, so I'll ask this way:
On an embedded system (Raspberry Pi) I use, there is a command invoked as follows:
$ pin-control poll 25
This command "polls"...
2
votes
1
answer
489
views
How can I take a sub-array in bash of the first N elements of a string array with elements containing spaces?
This question is similar to this one but it differs from it:
Consider this array with string elements which may contain spaces:
a@W:$ arr=("eins" "zwei" "eins plus zwei" &...
1
vote
1
answer
115
views
How can a bash script determine how it was started? [duplicate]
I am running various scripts on Debian 12.X currently with the following bash:
GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu)
I have the following methods/options to execute the script.
...
0
votes
2
answers
196
views
Bash script, execute while command isnt outputting value
I'm trying to write a bash script (very new to writing them btw) on a Linux system, and I basically want it to execute a command every say 5 seconds while another command isn't outputting a value.
So ...
-3
votes
1
answer
77
views
Rename files, remove the first four signs [duplicate]
There are many files in this format:
IMG_20240717_191421.jpg
IMG_20240620_165358_BURST001_COVER.jpg
IMG_20240624_173513_2.jpg
how can they renamed in Linux in Bash to:
20240717_191421.jpg
...
8
votes
1
answer
330
views
bash or ksh 'vi' mode - How to jump to end of history (most recent command)?
I am a long time ksh user, and use bash under duress - both in 'vi' editing mode.
One thing that has always niggled - after searching back through history for a command (.e.g <Esc>/needle), how ...
0
votes
5
answers
189
views
Move/mark files and the associated-part too
There is a folder with many files in it:
112.mkv
123.md5
123.mkv
221.mkv
467.mkv
aa1.mkv
abc.md5
abc.mkv
bbc.mkv
dde.md5
dde.mkv
ggh.mkv
....
xxy.md5
xxy.mkv
xxz.mkv
How can I move .md5 files and the ...
1
vote
1
answer
121
views
Why can't I run an interactive command in awk in a pipeline?
Consider the following commands:
$ awk 'BEGIN { system("ed") }'
q
$ echo hello | awk 'BEGIN { system("ed") }'
?
$
In the first case, I was able run ed and then stay in the editor ...
1
vote
0
answers
45
views
Alt + . but search for recent arguments
In bash when I press Alt + . (which is mapped to yank-last-arg), it will switch through arguments I used last to invoke CLI programs. Is there something like a search for it? Something like Ctrl + R ...
7
votes
2
answers
1k
views
Having trouble with #!/bin/sh -h as the first line in a bash script: /bin/sh: 0: Illegal option -h
I have two systems, the file is the same. It works in openSuse, but having trouble in Ubuntu. The top of the script says: #!/bin/sh -h
I updated bash to match on both systems to match what is on ...
0
votes
2
answers
177
views
Kill current bash shell and start a new one with some command
is it possible to kill/exit the current bash shell and start a new one with some command?
Something like
kill -9 $PPID && bash -c echo 'I started new!'
which does not work obviously.
0
votes
4
answers
170
views
Unusual symbols (parentheses) in the name of an environment variable
Situation. I work in Cygwin and there is a Windows environment variable that points to one of special folders:
ProgramFiles(x86)=C:\Program Files (x86)
As you see the variable name contains ...
5
votes
1
answer
144
views
Why can a list within a group be terminated with a space instead of a semicolon/newline as defined in the manual?
I am using the following version of the bash:
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Man bash states the following for the "group" compound commnad:
{ list; }
list is ...
0
votes
2
answers
69
views
create unique folder on startup with rpi cam
I have a pi cam on a pi zero that needs to run for the whole duration of being on. this pi camera module is being used in stratospheric launch and during our last one we were able to get videos back, ...
7
votes
3
answers
1k
views
How to properly protect .bash_history against truncation?
What can I do so that my .bash_history is only aver appended to?
I am working in a lot of environments, like singularity, apptainer, docker, podman and ssh with various settings and mounts. Each of ...
1
vote
3
answers
184
views
In bash, how to resolve what is actually going to be executed?
Bash has the built-in command "type", which indicates how each argument would be interpreted if used as a command name, for instance:
$ type myfunction
myfunction is a function
myfunction () ...
0
votes
2
answers
249
views
Extracting two (or more) related values from an array of JSON objects
Consider a contrived example using a JSON object such as this, where I want to extract the related id, firstname, and lastname fields for each of many array objects into shell variables for further (...
0
votes
1
answer
77
views
Why is bash expanding `[--x]` to `f` only when I’m in my home directory? [duplicate]
I'm observing a behavior in bash that only happens in my home folder. I haven't been able to reproduce it on any other machine or in any other folder.
$ echo --one [--two] --three
--one f --three
$ # ...
0
votes
1
answer
239
views
How to properly use taskset in a bash file when launching a program? [closed]
My bash script is as follows:
#!/bin/bash
sudo taskset 1 ./program &
When running it does nothing.
I am able to run the software normally with ./program.
I am attempting to do the behavior of &...
3
votes
2
answers
2k
views
bash shebang on macOS
On macOS, #!/bin/bash means a script will use the system bash, which is very dated (3.2, 2006 year) and (for myself, at least) mostly useless.
Then you can install modern bash using Homebrew or ...
3
votes
2
answers
185
views
What explains this very odd behavior of GNU grep interacting with buffering and pipes and how to stop it?
This is best illustrated with an example I feel:
{ printf 'foo\nbar\n' ; sleep 2 ; } | grep -m1 foo
{ printf 'foo\n' ; sleep 2 ; printf 'bar\n' ; sleep 2 ; } | grep -m1 foo
Both of these commands, ...
0
votes
2
answers
117
views
More than 1 option when launch script
Is there a way to pass more than one option when running a script?
My script has this case:
case "$1" in
b)
backup_start $1
;;
h)
_usage
;;
*)
echo 'Invalid option, -h for help'
;;...
0
votes
0
answers
230
views
single node Slurm machine, munge authentication problem
I'm in the process of setting up a singe-node Slurm workstation machine and I believe I followed the process closely and everything is working just fine. See below:
sudo systemctl restart slurmdbd &...
2
votes
1
answer
204
views
Redirect multiple Bash code files to SSH with Escaping (same SSH session)
Need to redirect 3 bash code files (possibly more in the future) to SSH, escaping them (within the same SSH session) to execute them on remote host.
Perfectly fine to list bash code files manually.
...
0
votes
1
answer
185
views
best way to load an .env file as exported variables
I find .env files to be funny things, is there a one line utility to do this:
set -o allexport
source .env
set +o allexport
I do not like flipping the shell settings like that, wouldn't it be nice if ...
1
vote
2
answers
108
views
recursive call to gnu parallel
I have a very slow samba share that i can access through wsl on my windows laptop.
On windows accessing the share doesn't take much time but on linux it is in order of seconds.
I basically replicate ...
5
votes
1
answer
162
views
Why does this bash for loop syntax work?
Why does this braced syntax work
$ for (( i=0; i<3; ++i )) { echo $i; }
0
1
2
when the manpage says the syntax is
for (( expr1 ; expr2 ; expr3 )) ; do list ; done
$ bash --version | head -1 # ...