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,446
questions
1
vote
1answer
27 views
/bin/sh evaluates string while Bash does not
When running the script below, I get two different outputs, depending on whether or not the shell used is sh or bash:
regex(){
echo 's/\(.* \)\(!\{0,1\}\)has(/\1\2MOCK_has(/g'
}
replace_builtins(...
0
votes
0answers
26 views
I could not execute my bash shell script -stat is not working
Script:
#!/bin/bash
USER=$(stat -c '%U' /home/user/Desktop/user_info.sh)
I would like to display the name of user who had created the script, but I'm receiving this error:
stat: cannot statx '/...
2
votes
1answer
36 views
Shell parameter expansion on command line does not work in bash script [duplicate]
Consider the following GNU shell parameter expansion on a bash command line, which is explained here:
$ a='hello world example'
$ echo ${a//+( )/_}
hello_world_example
However, that very same ...
0
votes
0answers
23 views
How to change integer in bash script according to written .dat filenames
In my bash script, I want to update a string according to new written filenames with .dat format. Here is what I am trying to do:
For example, I use a file named blabla_3200.dat in my bash script, ...
3
votes
3answers
55 views
Find all executable files excluding certain directories
I'm looking to find all executable files that are NOT in my $PATH.
Currently I'm doing this
find / \( -path "/opt" -prune -o -path "/var" -prune -o -path "/bin" -prune -o ...
4
votes
2answers
63 views
Why does quoting not protect filenames that start with “-” against mis-interpretation?
This might be a ridiculous post, but let's say we have a file called -|h4k3r|- , why doesn't this quoting work?
cat '-|h4k3r|-'
I thought single quotes remove meaning of all special characters?
...
1
vote
0answers
12 views
How to have `SPACE: magic-space` in ~/.inputrc without breaking Python 3 REPL?
If I put SPACE: magic-space in my ~/.inputrc file for configuring Readline, then I cannot enter SPACEs in python3 REPL (v3.8.7)
How can I use SPACE: magic-space in my ~/.inputrc without breaking ...
1
vote
1answer
27 views
Print variable value in echo statement
How to pass the variable value in echo statement?
testvar="actualvalue"
echo 'testing "${testvar}", "testing", "testing" ;'
Expected output:
testing "...
0
votes
0answers
42 views
bash: ls: command not found [duplicate]
I'm using VMWare, and running debian and whenever I run a new terminal my commands like "ls" seem to be lost.
I've tried the solutions listed here for instance: https://apple.stackexchange....
0
votes
0answers
10 views
Perform loop in HPC within multiple subdirectories one after another
I know this is a repeated question, but none of the suggestions I saw kinda fit what I wanted.
I'm working with an HPC cluster that has different modules to perform different actions for high ...
3
votes
3answers
136 views
How to assign two variables in a single for loop?
I have been running roary pangenome pipeline, where I need to write a script in for loop, For example I have a gff files as follows,
a.gff
b.gff
5.gff
101.gff
clustered_proteins
I need to run a ...
0
votes
0answers
38 views
Exporting environment variables in `.profile` with a bash and-list construct
I am using gdm3 as my login manager. Upon login, all the PATH modifications in my .profile are set and exported correctly. For example, lines like these below work fine:
if [ -d "$HOME/bin" ]...
0
votes
0answers
27 views
Is it possible to delete bad whitespace in multiple files of a project?
I have many software projects, in different languages. They are all in git repos. I normally use vim to program.
Recently, I enabled the vim spacehi plugin, and started noticing just how much bad ...
1
vote
3answers
81 views
Pipe result to multiple commands without writing on screen
I have a very big zip file (~10 GB) which I want to unzip, and the result has to be passed to two commands. The results of both subsequent commands have to be appended to a file, and
Currently, I run
...
0
votes
0answers
25 views
User defined function does not override system command
I would like to decorate (python-like) the conda command.
To do so, I tried the solution proposed in this Stack Exchange post.
It works to decorate other functions that I defined into my ~/.bashrc, ...
4
votes
3answers
555 views
How to fix infinite bash loop (bashrc + bash_profile) when ssh-ing into an ec2 server?
I mistakenly have source .bash_profile in the bashrc file and vice versa. Now when I tried to ssh into the machine (ec2), it will stuck at loading bash and get connection closed in a second. Is there ...
4
votes
1answer
309 views
Grep command not returning expected results for testing
I am attempting to do a comparison before I append conditions to a profile.d file
A grep -F 'TMOUT' /etc/profile.d/sh.local shows me exactly what I expect however the test is always showing true. In ...
0
votes
1answer
18 views
Kill tmux session after script is done
I have a bash script that I use to run a script inside a tmux session. It looks like this:
#!/bin/bash
session="myjob"
tmux new-session -d -s $session
tmux send-keys 'myscript' 'C-m'
The ...
0
votes
0answers
19 views
precedence of dircolors
I have a file on 2 servers. The problem is that ls --color do not display them the same way.
My file is a binary executable (with +x) and also have a LinuxCapability (CAP_NET_RAW).
ls --color on the ...
0
votes
0answers
44 views
Unable to understand the following shell script
I am trying to run a demo example given in the tutorial for running codes on a cluster computer.
Below is the example but I am unable to understand most of the statements -
#BSUB -L /bin/bash
#BSUB -J ...
3
votes
1answer
208 views
How to expand variables inside read?
In a remote CentOS with Bash 5.0.17(1) where I am the only user via SSH I have executed read web_application_root with:
$HOME/www
or with:
${HOME}/www
or with:
"${HOME}"/www
or with:
&...
0
votes
0answers
36 views
Bash simple process respawning daemon
Here is a script called simpled.sh:
#!/bin/bash
# if top is terminated i want it re-spawn
while :; do
top & tpid=$!
...
0
votes
2answers
29 views
Deleting files by checking file size in bash
I have two directories with files, one is the "source", the other
"destination". I would like to have a script that checks file
in "source". If file name and size matches ...
0
votes
0answers
24 views
how to exit in bash script from ProFTPD via netcat after some copy operations
I am connecting to a proftpd server via netcat
nc 10.10.239.150 21
Then I am doing some copy operations like
"The mod_copy module implements SITE CPFR and SITE CPTO commands"
After "...
1
vote
1answer
34 views
Can't use variables while feeding commands to bash through stdin
I need to run a small shell script using C popen call (which calls sh -c '<command>'). I can't get my script to work. I isolated the issue to this snippet:
#!/bin/bash
sh -c 'bash -xev -s <&...
2
votes
1answer
88 views
Debian 10: Setting PATH variable inside .profile does not work
I'm currently running Debian 10 Xfce and I would like to update my PATH variable to include /opt/bin and ~/.local/bin. As per these - 1 2 answers, I put the following lines in my ~/.profile file:
if [ ...
0
votes
1answer
23 views
write to file with echo/printf [duplicate]
How to write the following to a file
printf "*\n!.gitignore\n" > $datadir/.gitignore
-bash: !.gitignore\n: event not found
0
votes
0answers
23 views
Add WiFi Networks via Command Line
I have some known WiFi networks that I'd like to autoconnect to on Chromium OS. I know that I can add WiFi networks by SSID and password via the GUI, but I'd rather this be done via command line as ...
0
votes
1answer
36 views
bash pass “if” conditions as arguments?
is it possible to pass conditions that are checked in if clauses (https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html) as arguments to a function? The following does not work.
...
0
votes
0answers
26 views
Use parallel to speed up a recursive shell max function?
I needed a max function, to find the highest value in an array (of 350 int values), and being a friend of recursion, I tried a recursive approach:
max () {
len=$#
a=$1
b=$2
case $len ...
0
votes
1answer
24 views
Store sqlite3 table names in a bash array
I'm trying to figure out how to store the table names containing a geometry field from a sqlite3 database into a bash array that I can loop on afterwards.
Here's what I have but it's empty for the ...
-1
votes
0answers
25 views
Check if a host exists in ssh/known_hosts in a bash script; add a host only if it doesn't exist
I'm aware about how to check if a host exists in known_hosts
ssh-keygen -F github.com
I need to do it in a script. Namely, if a host exists - do nothing, otherwise - add it. How would I do it?
3
votes
1answer
79 views
How to not interpret string as command
I'm writing a script which should copy a client system and change some lines in a file.
There is a source file which has a line like:
$DB_HOST = "LegitDbHost";
And the destination file has ...
4
votes
1answer
331 views
why does tee refuse to work when launched remotely via ssh?
Where is my error? As an answer to a Stackexchange question, I tar a directory and transfer the archive to a remote system, where tee both deposits in a file and pipes it to tar for unpacking.
My ...
0
votes
1answer
31 views
Declare non environment variables and print them directly afterwards
I want to declare a few non environment variables and print them directly afterwards.
For example:
read domain &&
web_application_root="${HOME}/www" &&
domain_dir="${...
3
votes
1answer
133 views
#!_ perl is identical for #!/usr/bin/env perl?
My env says:
_=/usr/bin/env
and I have a question regarding the shebang, will the entry be correct if I type:
#!_ perl
?
0
votes
1answer
50 views
how can I remove specific columns and sort
I have a data like this
JW_ID ECK number B number T Number M K12 Conc(%) Yield(uM) Yield(ug/ml)
JW0002 ECK0003 b0003 thrB thrB 32 2.3 78
JW0003 ...
0
votes
1answer
46 views
awk: how to extract strings directly into bash array?
let's say I have this:
ARRAY=()
NUM_EXTRACTED=$(cat somefile.txt | awk '/magic/{i++; print > i "_extracted.txt"} END {print i}')
for (( i=1; i<=$NUM_EXTRACTED; i++)); do
LINE=$(cat &...
0
votes
0answers
13 views
Why does the history command show few commands and the .bash_history file is short even if HISTFILESIZE and HISTSIZE are ridiculously high?
I've read these two questions and the accepted answers on StackOverflow:
Unlimited Bash History
bash HISTSIZE vs. HISTFILESIZE?
However, I still don't understand why in any bash session I can see ...
-2
votes
0answers
34 views
What does echo asterick (*) mean in bash shell [duplicate]
Why does echo * prints all the files in the pwd, and with a leading dash (-)?
-1
votes
2answers
48 views
How to merge two files based on the matching of one column?
I have two files, B.csv:
1,AD
2,AB
3,AC
5,AF
7,AE
and C.csv:
1,x
3,z
5,y
How do I get this output:
1,AD,x
2,AB,
3,AC,z
5,AF,y
7,AE,
by matching the common column 1 in both of the files?
0
votes
1answer
22 views
In which file should it be best to put full debugging commands such as set -x (and also && complete -r)?
CentOS on a shared website hosting environment (I don't know the version) with Bash 5.0.17(1).
I use set -x to get maximal trace which often helps me handle errors; I normally use this command ...
-1
votes
2answers
38 views
How to match regular expression for below string in bash?
Below string I need to compare conditional operator. So only if 81.1.4_r4345 doing some operation or else matching with ādoneā then execute some operation.
vasanth-releases_81.1.4_r4345.tar.gz
...
0
votes
1answer
31 views
Example wanted: here string to fd
man bash features this redirection: [n]<<<word. With this explanation:
The result is supplied as a single string, with a newline appended,
to the command on its standard input (or file ...
0
votes
2answers
53 views
XOR in shell globs
Recently my hosting provider changed my public_html directory's name to www without me knowing it so this variable declaration web_application_root="${HOME}/public_html" no longer works.
For ...
1
vote
1answer
17 views
Storing for loop value in specific variable
so I've this shell script, where i take simple user input of IPs and store that into a text file.
MasterNode(){
echo "Enter number of Master Node below"
read -r numESM
for ((i=1; i<=${...
0
votes
1answer
27 views
In bash how do I set my path so that it finds the version of a command within the nearest relative path?
I have multiple git repositories on my machine. Each has a subdirectory called scripts/ with a script called cleancode.py.
Using bash (really git-bash), when I run cleancode.py I want it to execute ...
0
votes
0answers
15 views
/usr/share/bash-completion/bash_completion parse error
I am facing an error when run $ source ~/.bashrc. The error is /usr/share/bash-completion/bash_completion:1512: parse error near `|'.
bash_completion file (the first line is 1512) :
if ! [[ &...
1
vote
2answers
30 views
change permissions with find for specific files
Let's say I have a directory called permissions, that contains several files with different access rights.
What I want to do is if a file has access rights for others category, I want to accord the ...
0
votes
1answer
40 views
How to use BASH variable for AWK filtering results
I have this script, I'm getting first letter of my directory name, and I wan't to get directory names that start with the same letter and write to textfile. The for loop is looping list of directory ...