Questions tagged [bash]
Questions specific to GNU’s Bourne Again SHell, as opposed to other Bourne/POSIX shells. For questions about Unix shells in general, use the /shell tag instead. For shell scripts with errors/syntax errors, please check them with the shellcheck program (or in the web shellcheck server at https://shellcheck.net) before posting here.
26,105
questions
0
votes
2
answers
43
views
variable with hyphen in bash script
db1="community-service-data"
db2="ecomm-data-test"
drop_db="DROP DATABASE ${db1}"
sudo -u postgres -S /bin/bash -c "psql -U postgres -d postgres -c \"${drop_db}...
0
votes
2
answers
30
views
Bash script to compress nested directories not traversing correctly
I have a folder structure that looks like this:
0/1/2 and the numbers goes from {0..9}
I tried writing a bash shell script so that I can compress each of the folders like this:
#!/bin/bash
for i in {0....
0
votes
1
answer
43
views
Command not found on shell opening
Every time I open the shell, the first line on the terminal is "bash: n: command not found". I tried to look at /etc/profile, ~/.bashrc, ~/.profile and ~/.bash_profile but I was not able to ...
0
votes
2
answers
49
views
How to write an alias or bash script that renames a single file using the same syntax as the `ren` command of Windows?
I am a long-term a Windows user, so I find the syntax of the ren command both simple and convenient:
ren C:\pathTo\myFile\oldFileName.txt newFileName.txt
I want to be able to use the same syntax on ...
-1
votes
1
answer
58
views
when is .bashrc read?
I have been scratching my head trying to figure out why & how my ~/.bashrc file is read :/
I'm connecting to my Debian bookworm server through ssh. It's an interactive login shell.
According to ...
0
votes
1
answer
25
views
view a bunch of text files in the terminal [duplicate]
I've got a bunch of text files and pdfs in a directory.
If I enter
evince *.pdf
..all the pdfs open up in separate windows, I do this quite often.
When I enter
cat *.txt
..was pleasantly surprised ...
0
votes
0
answers
23
views
Evaluate command inside comparison in makefile [duplicate]
I would like to check if Linux system is ubuntu inside Makefile. What I do is:
copy_files:
if [ "$(head -n 1 /etc/lsb-release | cut -d '=' -f2)" = "Ubuntu" ]; then \
...
-1
votes
3
answers
64
views
Bash AND operator (&&) causes unexpected token if used within an if statement
I have found && to cause an error in bash when used within an if-statement. I am wondering if anyone has an explanation as to why this occurs and if there is a way to only run command3 if ...
6
votes
1
answer
324
views
Why does redirecting stderr interfere with bash's handling of $COLUMNS and the `checkwinsize` option?
I'm scripting a testing framework in bash and am running into a weird situation.
A couple things I'm doing in this post for illustration purposes:
Adding echo lines to the scripts; these are not part ...
0
votes
2
answers
73
views
How can I properly harden my system against some basic exploits like modifications of .bashrc?
I recently read a section on bashrc in the ubuntuusers wiki, in which a possible attack vector for privilege escalation is pointed out. Attackers, or malicious programs, could modify the bashrc or ...
0
votes
0
answers
18
views
Setting variable (ORACLE_BASE) for oracle user | Installing Oracle 23
Goal: Install Oracle 23 into /u00/app (and not the default, /opt/oracle).
Problem: Oracle installs into /opt/oracle (I assume the variable ORACLE_BASE etc. is not properly set / read for user oracle ...
0
votes
0
answers
26
views
tool or shortcut for copying part of the terminal
I apologize for the vagueness of this question but I saw this tool over 4 years ago at this point.
I remember working with a colleague at one point who knew a bash tool or shortcut that allowed him to ...
2
votes
1
answer
89
views
What is the limit of string size passed to bash -c?
I have the following command
echo '123456789#' | xargs -I% bash -c 'echo %%%%%%%%%%%%%%%%%%%%%%%%%'
It doesn't expand the last %. Apparently there's a limit on how long the string passed to bash -c ...
-4
votes
3
answers
61
views
show which of a list of filenames do not exist in a given list of directories
I have a long list of filenames like 20231121_150111.jpg... in a text file (one filename per line).
And another list of directories. Those directories contain files.
How can I check and show which ...
3
votes
2
answers
294
views
Remove duplicates in file (without sorting!) leaving the _last_ of the occurences
Following https://www.baeldung.com/linux/history-remove-avoid-duplicates, I
added
clean_history() {
awk '!a[$0]++' $HOME/.bash_history > $HOME/.bash_history.tmp && mv $HOME/....
4
votes
1
answer
962
views
Are there alternatives to alias I'm not aware of?
Today I started to notice weird behavior running a local command for a binary installed through Go. It seems the command is aliased, but consulting alias, it isn't.
The command buf somehow executes cp!...
0
votes
0
answers
25
views
Ghost string in terminal, string doesn't go away when using backspace [duplicate]
Linux shell problem on a server, bash 5.1.4(1)-release. Debian 11.10.
I have this on my command line right now after pressing the up arrow:
server# ctr plugins listkubeadm init --pod-network-cidr=10....
1
vote
2
answers
48
views
Need to make multiple multiline replacements in file
I have a file with multiple "paragraphs" like this:
<type>TRANS</type>
<attributes/>
<specification_method>rep_name</specification_method>
<trans_object_id/&...
0
votes
1
answer
69
views
How to put all function/command call output results to different corresponding vars: for stderr, for stdout, for string result and for return code?
I want to extend question How to store standard error in a variable and get general solution (bash function or functions) to extract from the called function/command together with standard error also ...
-1
votes
2
answers
171
views
Is it possible to translate bash to another human language like Arabic?
Bash commands are written in the Latin alphabet, is it possible to translate them to another language? In this case, Arabic. Arabic is written rtl and I remember I tried it using alias before when I ...
0
votes
0
answers
18
views
HISTTIMEFORMAT="%F %T " leaves oddly formatted date/time stamps [duplicate]
I am following this page to add date/time stamps to ~/.bash_history. Specifically, I added HISTTIMEFORMAT="%F %T " to my ~/.bashrc. Running exec bash didn't cause the date/time stamp to be ...
0
votes
0
answers
23
views
Best practice for running Intel MKL-dependent program as a systemd service
We have a systemd service that looks something like
[Service]
Environment="SOME_VAR=some_value"
# etc.
ExecStart=/bin/bash -c 'some command'
This service file, of course, is templated and ...
0
votes
1
answer
42
views
How to escape symbols in jq query?
I try to get area of JSON created by kubectl as another user.
runuser -l username -c "kubectl get ing -A -o json | jq --arg namespace "default" -r '.items | map(select(.metadata....
0
votes
0
answers
16
views
Using a backslash for an imap_user for mutt to send an email from terminal
I am trying to send an email from a shell script, and I am using mutt with sendmail. The imap_user has a backslash in the user name, and I have not been able to create the variable such that the ...
0
votes
2
answers
59
views
In bash/zsh, how to compare the metadata in two directories including . and excluding .. and the contents inside subdirectories?
Let's say that in a script, you have a directory $1 and its copy $2 and you'd like to compare the metadata (names, permissions, sizes, full modification dates, …) of the files and directories inside ...
3
votes
0
answers
51
views
Parted in scripted mode can increase but not decrease partition size
I am trying to use parted in scripted mode to resize a partition.
When I use the command to increase the partition size it works but when I use the same command but simply change the sector size to ...
0
votes
0
answers
17
views
bash variable problem [duplicate]
Apologies if this is nooby question but I am new to bash scripting and I couldn't find anything similar.
I am trying to use ffmpeg to re-encode a load of files and that requires I pull out some ...
2
votes
1
answer
133
views
Not working break in for loop in bash script for mounting a VHD
I'm not an experienced programmer. So maybe it's obvious for you why the break command in this script does not terminate the loop, but I can't see what is causing the problem. I want to use the script ...
0
votes
1
answer
28
views
X11 DISPLAY Configuration for SSH and Nvidia Optimus Graphics
There are one Intel integrated graphics and the other Nvidia graphics card in my remote box, where OpenSSH and X11 servers run on Ubuntu Desktop 24.04. The graphics drivers and utils are installed ...
1
vote
0
answers
42
views
Shell: Customizing the "Display all N possibilities? (y or n)" message
I already know that double-pressing the Tab key on Bash terminal will enlist all files and folders in the current working directory or, when appropriate, show Linux executables that I can run. I want ...
15
votes
5
answers
1k
views
How can UserA programmatically replace a text string within a file they own that's located in /etc?
A non-root linux user owns a text file that's located in /etc, where the non-root linux user does not have permission to create files. The non-root linux user can edit the file manually via vi without ...
1
vote
1
answer
113
views
What is PS1 prompt \[\e]0; vs \[\e]2; , it looks like one is for title name of tab, one is for title name of windows
here is my ps1
does someone know documents about this?
export PS1='\[\e]0;YYY@\h:\w\a\]\[\e]2;XXX\W\a\e[32;40m\]\[\e[36;40m\]$?\[\e[31;40m\]\D{%d}\[\e[35;40m\]\D{%H%M}\[\e[36;40m\]\W\[\e[32;40m\]>\[...
-3
votes
1
answer
81
views
How to split a number into unequal parts in Bash
Hypothetic example: There is a number that I will split into x parts (smaller numbers, in a range you see in the example).
I need a way, ideally a one liner for the Linux Bash, to split a large number ...
-2
votes
1
answer
71
views
How do I get lastlog timestamps in a generic format?
I have multiple Ubuntu machines in different locations. I want to collect the last login information from these machines. However, when I use the lastlog command, it returns dates in different formats....
0
votes
1
answer
37
views
gpg suddenly won't ask for password, "cancelled by user"
Suddenly, gpg won't ask for password when trying to decrypt file and instead says "cancelled by user". I have already tried..
GPG_TTY=$(tty)
export GPG_TTY
with no effect. I get the ...
1
vote
1
answer
26
views
`pushd` inside a while loop from file contents does not behave the same depending on read method [duplicate]
I'm pushing directories to my stack using a while loop that reads the contents of a file. I've tried two approaches that should be equivalent, but they behave different.
Approach 1
export ...
3
votes
2
answers
282
views
How do quotes work in bash regex regarding reserved characters?
Is there any special bash reserved characters in bash regex expression?
ex:
eg: if [[ $url =~ ^https:\/\/www\.youtube\.com\/playlist\?(.+&)?list= ]]; then echo "URL matches the regex"; ...
13
votes
3
answers
1k
views
Why does setting a variable readonly in the outer scope prevent defining a local variable with the same name?
I've found nothing about this in all my searching.
In the code below, Bar1 acts like I'd expect a "regular" variable to act (since help declare says that -g is only "seen" in ...
0
votes
1
answer
60
views
How to add categories in `free` command
I can extract the amount of memory used by running the following two commands from terminal:
free -h | awk '/^Mem:/ {print $3}' This shows the used memory, e.g. 4.8Gi.
free -h | awk '/^Mem:/ {print $5}...
-1
votes
1
answer
85
views
Bash deploy script only works the first time?
This script works perfectly the first time after starting it but when it's done it only continues to output zip_count: 0 afterwards. There seems to be some strange cache?
I run the script inside Git ...
2
votes
1
answer
48
views
Is there a way to `exec` a pipeline or exit shell after launching a pipeline?
I'm writing a shell wrapper script that is supposed to act as a pager (receiving input on stdin and performing output on a tty).
This wrapper prepares environment and command lines, then launches two ...
-2
votes
0
answers
75
views
Searching for a bootable USB Linux image to run a single bash script without user interaction
I'm looking for a lightweight Linux distribution that can be booted from a USB device with the sole purpose of executing a single bash script and then shutting down automatically. My requirements are:
...
2
votes
2
answers
56
views
set -o xtrace for just one line? [duplicate]
bash 4.4.20(1) on RHEL 8.4.
set -x ; ./blarge ; set +x works, of course, but it's just that much more stuff to remember to remove when you're finished. And gets sticky if you're running ssh, sudo, ...
-2
votes
4
answers
135
views
Getting output of /usr/bin/time in CSV format
I am using gawk to parse the output of macos' /usr/bin/time into CSV format as shown below. The problem is that gawk is returning the 'involuntary context switches' value for 'voluntary context ...
1
vote
1
answer
82
views
Shell script to run command and show output in notification box
I have command line utility that generates text strings, by running a command within the directory:
./Utility -P
it prints output on terminal. How to make a shell script showing the generated string ...
0
votes
1
answer
74
views
Redirect output to file but never create empty file
Is there a way to redirect output of a command that never stops to a file but only create the file once the command actually produces output?
Here is what I have so far:
target=logs/$(date +%Y%m%dT%H%...
-1
votes
1
answer
41
views
Variables concatenated with & are not defined in subsequent commands
I would like to know why the following shows an empty dialog box:
testvar="hello" & zenity --info --text "${testvar}"
While this works:
testvar="hello"; zenity --...
0
votes
1
answer
31
views
expect - repeatably send "\n\r" before a match expect
I need to netcat a U-Boot and perform NAND script and I use below script
spawn netcat -lup 6666
expect "Hit any key to stop autoboot"
#autoboot counts down from 3 to 0 and somehow one or ...
6
votes
4
answers
953
views
How to make Bash remove quotes after parameter expansion?
Consider the following Bash script:
args="--var 'lddpro_subdomain=prodcopy-lddpro' --var 'partner_subdomain=prodcopy-partner' --var 'model_design_subdomain=prodcopy-model-design'"
for x in ${...
1
vote
3
answers
72
views
Bash: sum a number present in N lines before a given pattern
I have a kdenlive project file (which is basically a xml file) with a lot of text clips inside. I need to bulk edit the x coordinate of a certain text that appears multiple times. This is an example:
...