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.
21,173
questions
1
vote
0answers
32 views
Make bash script to only output(type) without executing
I am trying to write a bash script that helps users type a command in terminal. However, I want the command to be left un-executed.
This is a quick example:
#!/bin/sh
function type_for_me() {
...
-1
votes
0answers
52 views
When do we need use “lastpipe” on bash?
In which case do we need the "lastpipe" feature in bash shell? I know it has the following function;
lastpipe
If set, and job control is not active, the shell runs the last command of a ...
0
votes
1answer
36 views
Find the frequency of unique names
I have subfolders of compressed csv (.csv.gz) files containing usernames and I'm trying to find the frequency of usernames.
The following bash pipeline gives me top users.
for subfolder in folder; do ...
0
votes
0answers
48 views
How to use SED or AWK to replace single static string in file?
I have a Bash script doing something. It contains a single static statement CHANGE_ME. What I want is to replace CHANGE_ME with the output from variable i.e.:
If $VAR is set to 192.168.x.x then ...
15
votes
1answer
455 views
How to use associative arrays safely inside arithmetic expressions?
A few Bourne-like shells support associative arrays: ksh93 (since 1993), zsh (since 1998), bash (since 2009), though with some differences in behaviour between the 3.
A common use is for counting ...
1
vote
2answers
55 views
How to concatenate “$@” or array to one string [duplicate]
I am trying to implement this helper bash function:
ores_simple_push(){(
set -eo pipefail
git add .
git add -A
args=("$@")
if [[ ${#args[@]} -lt 1 ]]; then
args+=('squash-...
3
votes
2answers
88 views
How to store and pass variable from ssh session to local machine
I am trying to retrieve shell variables generated on the remote machine in an ssh session.
echo -n "Enter the raspberry ip address you want to connect:"
read Rasp_id
sshpass -pthe@Donut ssh ...
1
vote
0answers
5 views
hash gvfs-backends results in “gvfs-backends : not found” (but it is installed)
Like the title says, I've got gvfs-backends installed but if I check that in a bash script:
if ! hash nmap gvfs-backends 2>/dev/null; then
echo "either nmap or gvfs-backends is not ...
0
votes
1answer
24 views
How to check, and list which extensions of by bash or python script used “extensions” are already installed or still need to be installed?
Some scripts use some additional stuff, like xdotool and other aditional stuff, which need to be installed on machine, which running a bash or python script.
Is there one script, or like this, which ...
1
vote
3answers
75 views
Split CSV files based on part of a column
I have a CSV file, file.csv, containing date and time like this:
id0,2020-12-12T07:18:26,7f
id1,2017-04-28T19:59:00,80
id2,2017-04-28T03:14:35,e4
id3,2020-12-12T23:45:09,ff
id4,2020-12-12T09:12:34,a1
...
1
vote
0answers
23 views
chroot fails to be executed more than once in a while loop
Description
Very same loop containing chroot command can be executed in terminal but can not be executed within a shell script.
Reproduction
Create a basic (or copy of your) rootfs in /mnt/myrootfs
...
0
votes
1answer
14 views
How to pipe text output to speech dispatcher?
I am not getting the command pipe to work for say and spd-say. Specifically, I want to pipe the output of fortune to spd-say, which seems more advanced than say. I tried the usual pipe construction
...
1
vote
2answers
76 views
Bash script is not working when using case statement to make decision
I have the following script that will schedule a job based on input from the file "schedule.csv". The file contains 7 fields and the $f7 field contains a flag that either has "NOT_S&...
0
votes
2answers
40 views
how to fail on first error when using bash as a template tool
I am looking for a better version of set -e. That better version should just workā¢. I briefly read BashFaq/105 and tried this insane approach, but I still do not know how to fail on error.
My use case ...
1
vote
0answers
23 views
How to capture the output of a Python program running in a subshell? [duplicate]
I'm trying to run a python program at startup by putting the following lines into etc/rc.local:
(cd /home/pi/CODE/weather-project/weather-station &&
python main.py) &>> log.txt &
...
0
votes
1answer
24 views
Unable to unmount /mnt which was mounted from a script
I have a startup script which executes the following commands:
sudo cryptsetup open /dev/sda3 dm_crypt && sudo mount -t btrfs /dev/mapper/dm_crypt /mnt && cd /mnt && ls -la &...
4
votes
4answers
936 views
Move (not copy) files from server to a local computer
I have a file on the Linux server that I want to move (not copy) to my local computer. However, I did something like as below after reading commands from stack overflow, however it just copies the ...
0
votes
1answer
12 views
Trouble getting bash tests working in .xsessionrc that work fine on command line
I've clearly become incapable of solving what seemed to me a simple rtfm issue. This line works fine on the command line (both zsh and bash):-
[[ -f ${HOME}/.xsessionrc.local ]] && . ${HOME}/....
5
votes
1answer
140 views
duration, start time and end time of a command after it is finished
I want to have a report like the following after a command finishes:
Start Time: 02/01/21 01:27pm
End Time: 02/01/21 02:29pm
Total Duration: 01 Hour 02 Minutes
I figured out I can get the Start Time ...
0
votes
2answers
62 views
Best practice for sending keystroke to FF Browser by xdotool
The question refers to bash, xdotool and Linux like Debian, Ubuntu, Mint.
xdotool github page:
https://github.com/jordansissel/xdotool
xdotool manpage:
http://manpages.ubuntu.com/manpages/trusty/man1/...
1
vote
1answer
28 views
Terminate python code without letting it finish if while loop condition is unmet in bash
I have this so far. The issue is, if pythoncode.py started before 8*60*60 seconds, then it can still continue running beyond 8*60*60 seconds until it finishes and only then will it stop. I need it to ...
0
votes
2answers
45 views
parameter expansion in file insted of cut
I wanted to do cut string on last delimiter using parameter expansion.
name='ls $(Build.ArtifactStagingDirectory)/drop/abc.jar'
string1="${name%/*}"
string2="${name##*/}"
echo &...
0
votes
1answer
42 views
warn and provide the number of files that will be affected by rsync command
My rsync's --dry-run output is:
% rsync --dry-run -avi --delete-after /home/blueray/Documents/rsync-test/src /home/blueray/Documents/rsync-test/dest
building file list ... done
.d..t...... src/
>f....
2
votes
1answer
16 views
Use xmlstarlet to remove an entire element that matches an attribute value?
My question is similar to sed - Delete XML node containing certain element - Unix & Linux Stack Exchange. Trying to implement the suggestions there has kept me busy all day, but I haven't managed ...
4
votes
2answers
149 views
Why does printf byte formatting fail when executed under `dash`?
The ASCII hex code for a zero is 0x30. Hence, you can print a zero by doing printf '\x30', and it will print a zero.
If you put this into a shell script called myScript.sh, and then execute ./myScript....
1
vote
2answers
77 views
I sourced my .bashrc file and now get: ā-bash: /usr/bin/whoami: Argument list too longā so that I can't execute any commands. Using ssh
I use an external HPC system via ssh. Today I tried to install "ASE" a Python code for dealing with atoms. I followed instructions to modify my .bashrc file but kept getting ...
2
votes
2answers
58 views
Creating directories and files recursively with bash expansion
So I just have learned about bash expansion...and it's cool :) now I want to create some good plans for next year (just as an example).
I can't figure out how you can create folders and create some ...
0
votes
1answer
34 views
How to copy a file to a path with a space in it in Bash Script? [duplicate]
Sample script to copy /tmp/template.txt file to any directory as specified in $1.
copy_script.sh
if [ $# -eq 0 ]; then
echo No Argument
echo "Usage: $0 <path>"
else
cp /tmp/...
5
votes
4answers
128 views
I want to terminate a python script if it doesnāt output anything to the terminal in 2 min
Here's my current bash which is just a loop
while true ; do
python3 /Users/Name/Desktop/pythoncode.py
done
I want to terminate pythoncode.py if it doesnāt output anything to the terminal in 2 ...
1
vote
1answer
21 views
gnu parallel: Warning: This job was killed because it timed out
I am running multiple commands on remote hosts. if I pass my command directly in my ssh script it's working, once I pass it from another script as an argument it gives me a result of my first host and ...
0
votes
1answer
21 views
while read loop not working inside alias [duplicate]
In Bash, the following command
echo foo | while read line; do echo $line; done
outputs foo; however, the following
alias bar="echo foo | while read line; do echo $line; done"
bar
outputs a ...
5
votes
2answers
233 views
“grep string | grep string” with awk without pipe [duplicate]
Is there a way to do:
output | grep "string1" | grep "string2"
BUT with awk, WITHOUT PIPE?
Something like:
output | awk '/string1/ | /string2/ {print $XY}'
Result should be ...
-2
votes
1answer
50 views
Difference between curly braces and brackets in command substitution - ${} and $()
I was trying to uppercase the output of date command in bash (GNU bash, version 5.0.17).
date +"%d%m%Y"
# 30Dec2020
I came across ^^ operator for uppercase which is used as
DT="$(date +...
-2
votes
1answer
32 views
Confusing use of && and | operators
I haven't been able to dig up documentation regarding this particular use of | operator. Can anyone explain what | does in this particular context?
0
votes
2answers
96 views
Is there a simple Bash command which just sets the exit code?
When testing exit code handling it is often useful to trigger an arbitrary exit code anywhere within the script, as a sort of hack approximating a Python Mock with side_effect = Exception(). For exit ...
0
votes
3answers
59 views
Remove album art from MP3 files from them command line
I want to remove the album art that is embedded in a bunch of MP3s (thousands) and cannot find a command-line way to do this., I can add art via lame and I can add or remove pretty much any other tags ...
7
votes
1answer
311 views
Why is the file descriptor opened and read only once?
Why can it be used only 1 time when we open the file descriptor ourselves and redirect it to stdin?
Please take a look at the example below to understand what I want to say. After reading it once with ...
0
votes
0answers
38 views
How to notify if a file exceeds a certain size
Based on some work i found on other posts I created this script but the only response I get is "setting up watches" and "watches established"
#!/bin/bash
exec 3>&1 4>&...
-1
votes
2answers
47 views
Showing error while using mount in Ubuntu 18.04/Linux
Commands want to execute ( or update_image.bash file) :
#!/bin/bash
sudo losetup -f /dev/loop0 floppy.img
sudo mount -t /dev/loop0 /mnt
sudo cp -T src/kernel /mnt/kernel
sudo umount /dev/loop0
sudo ...
-1
votes
1answer
46 views
Bash function to run arbitrary command on two branches
I want to write a bash function that executes an arbitrary commands on multiple branches.
compare_command () {
branch2="$1"
shift
command="$@"
echo $branch2
...
-1
votes
0answers
31 views
Setting cursor color in vi-mode [closed]
I've been trying to set my cursor color in vi-mode to one my terminal colors.
I have the following in my bashrc (taken from Different bash prompt for different vi editing mode?).
bind "set vi-cmd-...
3
votes
1answer
29 views
How to read binary data including zero bytes using BASH builtin read?
I found bash ignores binary zero on input when reading using the read buildin command. Is there a way around that?
The task is reading from a pipe that delivers binary data chunks of 12 bytes at a ...
25
votes
8answers
2k views
Allow bash script to be run as root, but not sudo
I'm new here and new to bash/linux.
My teacher gave me an assignment to allow a script to be run only when you're "really" root and not when you're using sudo. After two hours of searching ...
0
votes
1answer
24 views
What Bash feature is used when replacing strings in a variable within curly brackets?
In https://stackoverflow.com/questions/12174947/removing-part-of-a-filename-for-multiple-files-on-linux#12175160, a piece of sample code uses a Bash feature where string manipulation on the contents ...
0
votes
1answer
31 views
how to copy .bashrc file for several users? on solaris
I used this script
#!/bin/bash
X='$( cat etc/passwd | cut -f1 -d: )' #All-users
For X in /export/home/*/.bashrc ; do
echo "$HOME/.profile'" >> $X
$X 2>/dev/null
done
source $X
...
0
votes
1answer
24 views
Prevent ``/etc/profile.d`` from being sourced on login
I just noticed my environment is behaving erratically because at
some point this alias got introduced in
/etc/profile.d/vim.sh:
$ command -v vim
alias vim='__vi_internal_vim_alias'
Looking through ...
0
votes
1answer
14 views
Executed Bash script not reading a file if cwd is elsewhere
I have a script in home/me/bin with an alias in my .bashrc which reads a file xs.txt also in home/me/bin. I want the script to count the lines of the contents of xs.txt; however, if I run my alias ...
-1
votes
1answer
27 views
Can I resume a job in the background and redirect its standard output to a file? [duplicate]
I have stopped a job by pressing Ctrl-z. Can I resume the job with bg, yet have the output redirected to a log file instead of stdout? Doing the following:
bg > log.txt
Only writes the initial ...
2
votes
4answers
199 views
cut empty lines from a file (bash script) [duplicate]
I am trying to get rid of all the empty lines from a file but I do want to preserve "\n" after every non empty line.
Problem: command works correctly if used in CLI but as soon as I use any ...
0
votes
3answers
31 views
How to subsitute contents of a file contained within the output of a variable
I have searched for variable syntax and other commands but I cannot get through.
I have some folders created with btrfs snapshots.
Within each folder there is a file called /etc/fstab that I need to ...