156,878 questions
0
votes
0
answers
81
views
Why bash's export directive doesn't work with eval directive? [duplicate]
I can't figure out why this code outputs VAR= when it should output VAR=val.
Any ideas?
#!/usr/bin/env bash
arr=(
"export VAR=val"
"echo VAR=$VAR"
)
for it in "${arr[...
0
votes
1
answer
80
views
playing sound file when bash script is ran by cron task
below is a bash script that works fine tested in my terminal the sound is played but when it is actually ran in a cron scheduled task there is no sound only the dialog is shown
#!/bin/bash
export ...
0
votes
0
answers
85
views
shopt nullglob seems to interfere with associative array? [duplicate]
Associative array appears to behave differently when nullglob is set:
shopt -u nullglob
unset aya
declare -A aya
aya["10"]="hi"
echo ${#aya[@]}
unset aya["10"]
echo ...
1
vote
1
answer
101
views
Remote Command Execution and Cleanup
Goal
Execute long running command remotely with the following capabilities:
We can kill the task with its PID
If task completes, its exit code should be written to file
Task should be detached from ...
0
votes
0
answers
66
views
using bash "read" command twice in a loop [duplicate]
I have a bash script where I use the read command twice:
1st usage: read text file line by line
2nd usage: asking user in the loop to confirm something
However, the second usage is completely ...
1
vote
1
answer
85
views
Printf command stops working when DEBUG trap is active?
When I set a DEBUG trap in Bash like this:
set -o functrace
trap 'echo "# $BASH_COMMAND" >&2' DEBUG
Suddenly, this function stopped working:
getBase() {
local base="$1"
...
1
vote
2
answers
178
views
How can I create a Python venv within a Bash script and then activate it immediately, all from a VS Code profile's settings.json?
I have this profile settings.json:
{
"terminal.integrated.profiles.osx": {
"python-venv": {
"path": "/opt/homebrew/bin/bash",
...
-10
votes
4
answers
209
views
Join multiple paths in Bash [closed]
i need Python's os.path.join function in Bash
it should be implemented as a pure Bash function
with zero dependencies on external tools like sed
on error, the function should return 1
but it should ...
0
votes
2
answers
154
views
Output stderr to terminal and stdout+stderr to file [duplicate]
From a command I want to redirect stderr to tty and stdout+stderr to a file. How can I do that?
e.g. for this:
cmd.bash
#!/usr/bin/bash
echo "Hello World"
echo "this is an error" &...
-4
votes
2
answers
117
views
append to file and remove first lines if more than N
I want to edit a file in a single command to append a line and remove first lines if more than N.
The furthest I got was to use sed:
sed -i -e "\$a\[$(date '+%F %T')] $1" -e :a -e "\$q;...
0
votes
3
answers
134
views
Bash script to delete oldest file causes error - but used to work before
I have this part of a script which worked for a couple of weeks, but now stopped working:
find /media/2C1F-444F/usb_musik/newMusik -type f -printf '%T+ %p\n' | sort | head -n 5 | awk '{print $2}' | ...
1
vote
2
answers
118
views
Bash Indirect Expansion
This example is from AI, but it doesn't appear to work. Before sending it as a bug to the gnu bash team, I would like to validate that this is a bug.
If I declare a set of associative arrays such as ...
0
votes
1
answer
70
views
Build nacos Error bin/docker-startup.sh: line 104: exec: -X: invalid option i use official document
i add nacos-plugin to nacos-2.0.3,but when i create images error,i modify dockerfile,
when i build nacos ,occur pring.config.name=application,custom --logging.config=/conf/nacos-logback.xml --server....
-2
votes
1
answer
102
views
My script works when ran manually but fails in cron. Where am I going wrong?
I've written a script to reboot my router every other day. It works as intended when I test it out manually but doesn't work properly in cron.
#!/bin/bash
c=$(ls -a /tmp | grep -o ".daycount*&...
5
votes
1
answer
80
views
Why it is not possible to trap SIGQUIT in Bash in docker build?
Kindly consider the following dockerfile that uses bash image and runs a bash to trap QUIT and then shows that QUIT trap. It should show the string "something".
ARG VERSION=latest
FROM bash:$...
0
votes
2
answers
94
views
partclone print progress every nn seconds to a new line in bash
I have searched everywhere but could not find a solution hence posting here. Partclone prints the status / progress on the same line and it keeps updating this line, I want the progress to be ...
2
votes
1
answer
77
views
Bash process id and parent process [duplicate]
I am using the following Bash script. What I don't understand is why the parent PID and the current PID are not matching. For example:
#!/bin/bash
my_background_function() {
echo "Starting ...
1
vote
2
answers
111
views
How to append each line of a file to the corresponding line of another file?
I have 2 files of data and I'd like to add data from one file to the other, but rather than appending to the file as a whole I'd like to append line-by-line.
E.g. I have:
ip.txt
192.168.0.10
192.168.0....
0
votes
1
answer
170
views
Curl POST request error: Unknown content type: application/json
SOLUTION: Got rid of the --header flag, added json= to the beginning of the data set.
I'm attempting to update some settings in a qBitTorrent Docker container via the WebUI's included API. I've ...
4
votes
1
answer
85
views
How to make it so a background program is killed after the terminal that called it is closed?
I have a small script to run a node.js server and open it up on the browser right after, problem is that if I close the terminal that this creates, the server keeps running in the background of my ...
1
vote
1
answer
106
views
Best practices for SLURM job pipeline with wrapper scripts - avoiding complex job ID extraction
I'm building a SLURM pipeline where each stage is a bash wrapper script that generates and submits SLURM jobs. Currently I'm doing complex job ID extraction which feels clunky:
# Current approach
...
0
votes
1
answer
92
views
Trying to XOR for a string in hex using radare2 in a DOS executable on Linux. Am I doing this right?
I've tried to build a shell script that iterates through all possible XOR obfuscations of a string within a DOS binary executable, parsed using radare2 in hexadecimal, and find it. Am I doing this ...
1
vote
2
answers
125
views
Slicing bash array indices
Problem: How to get a slice of a bash array's indices?
$ array=('a' 'b' 'c' 'd')
$ echo ${array[@]:0:2}
a b
$ echo ${!array[@]:0:2}
bash: a b c d: invalid variable name
What is exactly happening in ...
-1
votes
2
answers
195
views
Automatically Converting Windows Shortcuts to Linux Links using Bash
Mid-switch to Linux I realized I've a large personal and work archive that has over 1100 Windows Shortcuts(.lnk) of both files and folders and it is unusable without them and have been trying to ...
-2
votes
1
answer
103
views
mpc status | awk 'NR==1' save result as variable [closed]
in a terminal mpc status | awk 'NR==1'
$ mpc status | awk 'NR==1'
Ramones - Pet Sematary
would show me which song is playing
how to keep this as a variable to use it later?
found in the net some ...
2
votes
5
answers
114
views
sed: skip match if has already been replaced
I am playing with text automatically converting to markup.
I can make sed enclose some inline code:
echo "this is inline_code test 1_2 (_)" | sed -re "s/([a-z0-9]+_[a-z0-9]+)/\`\1\`/g;s/...
2
votes
2
answers
188
views
Function doesn’t update global array
I wrote a simple quick sort algorithm. When I run it, it echoes the same array back.
#!/usr/bin/bash
arr=(1 8 3 9 4 5 7 2)
partition() {
local low=$1
local high=$2
local pivot=${arr[low]}...
0
votes
1
answer
67
views
Why does `set -e` differ depending on how a called script fails? [duplicate]
I thought I understood -e but unless this inconsistency is a bug then I don't...
bash --version # GNU bash, version 5.2.32(1)-release (x86_64-unknown-linux-gnu)
cat >set-e-exit.bash <<'EOF'...
-1
votes
0
answers
35
views
How to open interactive SSH shell from script after running commands [duplicate]
At my workplace, VMs I SSH into require 2FA. Until I run ssh-copy-id, they also require a password. Currently, I have a script that runs ssh-copy-id then ssh, which results in two 2FA if I've copied ...
2
votes
4
answers
162
views
Is there a standard way to grab "values" from a "key" in bash?
I'm learning bash from a book and just wanted to see if there is a more efficient way to do this.
The output of cat /proc/acpi/wakeup is multiple lines, but I only care about this one:
GPP0 S4 ...
1
vote
1
answer
130
views
find: paths must precede expression: `\)'
I'm trying to implement simple wrapper for find to find files, links or both. Here is path of my code:
typeArr=()
if [[ "$type" == "f" || "$type" == "l" ]]; ...
1
vote
2
answers
84
views
qpdf encryption works in a for loop but fails with GNU Parallel: "unrecognized argument --print=none"
I am trying to batch-encrypt a set of PDFs using qpdf.
This works fine in a shell for loop:
for f in *.pdf; do
qpdf --encrypt "" "" 256 --print=none --modify=none --extract=n --...
1
vote
1
answer
148
views
How to change cursor shape depending on Insert or Normal mode when using `set -o vi` in `tmux`? [closed]
I would like to use set -o vi in my terminal.
However, I find myself getting confused on whether I am in insert mode or normal mode, since the cursor does not switch between a vertical line (insert) ...
1
vote
2
answers
123
views
How to read multiple bash variables containing spaces in values with the read builtin?
I'm trying to read 2 lines into two bash variables but those lines contain spaces.
Here is how those two lines are generated :
$ url=https://www.youtube.com/watch?v=c5U4D-Hn5Vg
$ yt-dlp --no-config --...
0
votes
1
answer
115
views
How to accurately detect run time in linux?
I have a binary sample (VirusShare_00238010bb86fd0bf4014c3d19f114b2) used in my research that behaves oddly when I try to measure its runtime
When I run it directly with /usr/bin/time, it exits almost ...
0
votes
1
answer
88
views
bash aws cloudshell for loop only returns the first item, then hangs [closed]
Pulling my hair out, looping over 100K S3 files in AWS Cloudshell; to copy in place to kick off a file processing lambda. when I run my for loop with an aws s3 cp on the dataset I want to, the for ...
2
votes
0
answers
38
views
Bash equivalent of csh's $?var [duplicate]
csh and tcsh use the syntax $?var to test whether a variable is set or unset. $?var or ${?var} expands to 1 if $var is set (even if it's set to the empty string), or to 0 if it isn't. This 0 or 1 ...
1
vote
1
answer
90
views
Bash variable string substitution to '*'
I tried to figure out how to replace a string variable to '*'
a="bamtools/*/*bam_metric.summary.tsv"
## not working
echo ${a/*\*/}
I would like to see bam_metric.summary.tsv after ...
0
votes
2
answers
86
views
Distinguish permission denied from nonexistent files
If you run:
stat /etc/fake_parent/fake_folder/fake_file
then stat will report "No such file or directory". But if you run:
stat /etc/sudoers.d/fake_folder/fake_file
then stat will instead ...
0
votes
1
answer
97
views
Script looking for a file that doesn't exist
the start of my script is:
for i in **; do
FileName=$i
FileType=$(stat -c %f "$i")
if [ "$FileType" = "41ed" ]; then
FileType="D"
else
...
0
votes
4
answers
183
views
Creating bash case patterns from a space-delimited string
I have a bash script that uses a space-delimited string to keep track of all the possible valid arguments:
FRUITS="apple orange pear banana cherry"
I use a bash case statement because some ...
1
vote
0
answers
48
views
SLURM sbatch output file with cluster name
Given:
#!/bin/bash
#SBATCH --account=project_XXX
#SBATCH --job-name=test
#SBATCH --output=/scratch/project_XXX/ImACCESS/trash/logs/%x_%a_%N_%j_%A.out
#SBATCH [email protected]
#SBATCH -...
-3
votes
1
answer
103
views
Does anyone have a nice concise way of parsing bash function arguments and options? [closed]
Here's my current preferred method that I normally put at the top of functions:
#!/usr/bin/env bash
unset opts args
for arg; do [[ ${arg:0:1} == "-" ]] && opts+=("$arg") || ...
3
votes
4
answers
179
views
How to remove the extra space between two cut commands?
I was making a script for a challenge I saw a while ago, which asks: "extract the 2nd ASCII character from every line in input if the line characters are less than 7, 2nd and 7th if more", ...
0
votes
1
answer
67
views
Bash Syntax error with multiple two paramter mkdir [closed]
mkdir ./output
for year in {1990..2025}
do
mkdir ./$year \n
for week in {1..52}
mkdir ./"$year"/"$week" \n
done
done
gives syntax error on second call of mkdir ...
-2
votes
1
answer
73
views
Bash script on Ubuntu WSL with odd concatenation behavior using fzf and a word list [duplicate]
This script behaves perfectly fine on a real Ubuntu system.
#!/bin/bash
set -Eeu -o pipefail
word1=$(awk '{print $2}' eff_short_wordlist_2_0.txt | fzf)
echo "${word1}"
word2=$(awk '{print $...
1
vote
1
answer
69
views
Git Bash (MINGW) on windows: Cannot use parameters starting with /C [duplicate]
Try doing the following:
echo '/C=UK/'
This will output /C=UK/ literally. However, if I try using it in a program...
openssl req -subj '/C=UK/' (...)
Then I get the following:
req: the ...
2
votes
4
answers
148
views
stop a BASH script from exiting a loop following a terminal command [duplicate]
I am trying to write a BASH script that will issue commands to ADB to remove packages from a list of packages in a file. However, my script leaves the loops that iterates over the file if the script ...
0
votes
2
answers
76
views
Trapping input from user [duplicate]
I'm in the process of creating a bash script. I need input from the user. Here's a snippet:
printf "Press$red Y$green to continue,$red N$green to Abort,$red D$green for Dry Run or$red H$green ...
1
vote
1
answer
125
views
How can I automatically run a script after each bash command I run, that sees that command I just ran?
After I enter command in an interactive bash terminal, e.g. foo | bar, I want to automatically and silently run a script after, e.g. my_script.py "foo | bar". I can almost do this with ...