Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

1
vote
1answer
28 views

Error in Bash Script Nested Conditional Statements

Getting the error below as I'm sure my syntax is off somehow with the nested conditionals. Basically I don't want to send an empty message to my telegram_bot which throws an error if it detects empty ...
0
votes
2answers
64 views

syntax error near unexpected token 'elif'

I get the error on the 7th line. Any ideas? I checked for spaces and there are none. #!/bin/bash if test $# -eq 0 then echo "No arguments" elif test $# -eq 1 echo "$1" elif test $# -eq 2 ...
0
votes
1answer
37 views

How to add Tab Space in between awk / sed command in bash

I have two separate lines being consolidated onto one line. The issue is that at the current moment, it is being separated by a space. I would like to know how I can separate the two lines into one ...
1
vote
1answer
25 views

Rsync is syncing $HOME instead of the provided directory

Any idea why this rsync command could be syncing contents of a home directory instead of the actual path provided? echo "\${REMOTE_STORAGE_PATH}: ${REMOTE_STORAGE_PATH}/files/" rsync -zqr -e "ssh -p ...
0
votes
1answer
89 views

bash how to check size of the swap

I have a bash script which checks if a swap exists or not and if it doesn't exists, then it creates one; if free | awk '/^Swap:/ {exit !$2}'; then echo "Have swap, skipping" sudo swapoff -a ...
6
votes
2answers
334 views

bash aliases do not expand even with shopt expand_aliases

I want to run an alias inside a bash -c construct. The bash manual says: Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt In ...
0
votes
0answers
28 views

Reusing the last N lines in terminal

I'm writing a Bash script that makes a series of build commands visiting each of subdirectories. It mainly prints the logs from the build command, but it occasionally prints logs by itself, like what ...
0
votes
1answer
60 views

tmux not working with Bash [closed]

I am using macOS with tmux downloaded from source and Homebrew. When starting tmux both times I installed it, it gives me a blank screen with the bar at the bottom. I can use tmux commands but a bash ...
2
votes
1answer
34 views

How to use bash process substitution for a sftp private key file?

With my team, we need to share a few private keys and we store them in a pass repository. So instead of extracting the key and storing it in cleartext on the filesystem, I am looking for a way to ...
1
vote
2answers
127 views

How to compare two dates in bash script?

I am trying to compare todays date with the last modified date from a file. DATE=$(date +"%F") LASTMOD=$(stat $i -c %y); LASTMOD_DATE=$(cut -d' ' -f1 <<<"$LASTMOD") if [ "$LASTMOD_DATE" -...
-1
votes
1answer
44 views

Creating a Shell Script that deletes specified files in git repo

How would I create a script that would take a list of file names in a git repository (I was planning on using a .txt file with one file name on each line, but I am open to other suggestions) as an ...
1
vote
2answers
56 views

How can I change user inside function

I have a bash script on centos7, and I need to execute some commands as different user. But it seems sudo works as expected outside function and didn't work inside bash function. I run script as ssh ...
1
vote
1answer
44 views

send cron output to a filename that is derived from the program name

I tested the following with no luck. Any other ideas? #cron test, I would like to generate a stderr logfile name based on $0, but $0 value is "/bin/bash" #why do I want this? Because some program ...
0
votes
2answers
35 views

eval ssh command in variable

I am trying to execute commands via ssh remotely. But I have the calling command saved as variable such as: SSH_CMD="ssh -i path_to/identity" SSH_USER=user SSH_DST=server dir1=dir1 dir2=dir2 now if ...
4
votes
2answers
269 views

Aliased pipeline using head and cut

I'd like to create an alias to have a quick view of the table format files with comma separator: alias thead='head | cut -d, -f1- | column -s, -t' Later using it like this thead file.csv However, ...
2
votes
1answer
44 views

map ls -a to ls -A

I have tried to made an alias ls -a = ls -A, ending up with: bash: alias: `ls -a': invalid alias name I want ls -a without . and ... How can I get that?
1
vote
1answer
33 views

Alias to prompt for tab completion for a set of directories?

Is it possible to make a bash alias, which would be able to prompt me for tab-completion of a set of directories? let's say I have a number of source code controlled projects, main path of each ...
0
votes
2answers
47 views

Is there a standard array name for $@, like ${ARGS[@]}?

I don't expect it to be modifiable. That is, I don't expect it to enable something like ARGS=("${ARGS[@]}"), but only to simplify something like myarray=("${ARGS[@]/--unneeded-argument/}"). Note that ...
0
votes
1answer
32 views

Re-mapping EOF (ctrl+D) in bash

If I'm in a bash shell, because of how stty is set up, typing Ctrl+D will send an EOF to bash, which will then do pretty much the same thing as the command exit does, and exits the shell. I'm ...
2
votes
2answers
62 views

Read from file then use for to split fields but honor quotes

This what I am trying to get in a bash shell script: for a in "1" "2 3" "4 5 6" do echo "a: $a" done a: 1 a: 2 3 a: 4 5 6 But the quotes are treated differently when you use a variable: echo "\"1\...
1
vote
1answer
30 views

Unable to start new gnome terminals from shell script when remotely logging in through ssh

Background: I SSH into my Linux machine via Putty on my Windows machine. I am running VcXsrv on Windows and forwarding X over SSH. This is all working as expected. I am running into issues opening ...
2
votes
1answer
29 views

I want to add a variable for the results from the formula of one variable and the beginning of another variable

This is the file name in question OD_Orders_2019-02-19.csv I am trying to create a bash script to read into files with yesterday's date on the file name while retaining the rest of the files name. I ...
0
votes
2answers
34 views

Batch renaming when the destination file need spaces

I need to rename a batch of markdown files with random names based on the title name, and the destination name need to be with spaces (this is a spec). I'm trying this one-liner for i in *; do mv $i $...
36
votes
3answers
4k views

Why is opening a file faster than reading variable content?

In a bash script I need various values from /proc/ files. Until now I have dozens of lines grepping the files directly like that: grep -oP '^MemFree: *\K[0-9]+' /proc/meminfo In an effort to make ...
0
votes
1answer
32 views

Chaining (i.e. sequentially running) bash scripts and setting environment variables within these bash scripts

Apologies if this is a stupid question, but I'm having difficulty getting my head around this. I have a file hadnodes.conf (setting node IP addresses for AWS VMs) > ls -lista hadnodes.conf ...
0
votes
1answer
43 views

Running script using at command doesn't work entirely

So I did a bash script that read a file in which are directory numbers and then proceeds to run pgloader over them. I looks like this #create logs for pgloader Date=`date +%Y_%m_%d` LOGS="...
1
vote
0answers
54 views

How to capture ssh commands on the fly with auditd

There is any way to capture ssh commands executed by an user, on the fly with auditd? The image exemplifies a case where a user send a command and auditd capture the command in execution time. At ...
0
votes
1answer
35 views

Looping 'find…-type f': Getting filenames by dir/subdir, ouputing a txt with name dir_subdir_fname.txt

My data structure is something like: dir1/subdir1_level1/subdir1_level2.../subdir1_leveln dir1/subdir2_level1/subdir2_level2.../subdir2_leveln ... Which means that the levels I'm working with are ...
1
vote
0answers
80 views

Storing values from loop and comparing them every time run the script

Description Hello, I'm trying to loop over certain commands and save their outputs into a file, and while looping through those commands, also check the file which we saved their outputs so we can ...
0
votes
1answer
32 views

The -p option in the bash history command?

From man bash, history -p arg [arg ...] ... -p Perform history substitution on the following args and display the result on the standard output. What does 'history ...
0
votes
1answer
36 views

file not getting created after redirection and “cp: cannot stat” error

In the bash script below, I check whether a file exists or not and then if it doesn't, I create it and write a simple snippet to it. It worked once(I don't know why), but I guess I have changed ...
2
votes
1answer
53 views

Reverse the order of file matching as arguments

I have a program that reads files in a certain order. For this example, they all have the same extension, txt {program_name} {input 1...} {output} I happen to name the output file name "a.txt" so it ...
3
votes
1answer
81 views

Can bash read the “Date Added” of a file/directory?

With programs like stat you see access, modify and change, but with Finder on macOS there is a "Date Added" field. Can I get the "Date Added" for a file or directory displayed in the Terminal, like ...
1
vote
1answer
29 views

Automatically returning after a command

I'd like to have return pressed twice after I run a command to make the terminal easier to read. [root@localhost ~]# fdisk -l /dev/sda1 Disk /dev/sda1: 1 GiB, 1073741824 bytes, 2097152 sectors Units: ...
2
votes
1answer
42 views

Why does trap ERR behaviour differ over bash versions 3 and 4?

Background Execute the following code in bash 3, 4 and 5 respectively, and you will get differing results. (function handle_error () { echo ERROR; }; trap handle_error ERR; (exit 1)) Imagine that (...
0
votes
2answers
39 views

How to create a shell dictionary from an output of a python script?

My python scripy print a string by print("declare -A gaps=( [2019-2-24]=4 )") and I can run declare -A gaps=( [2019-2-24]=4 ) on a bash shell to create a dictionary named gaps. In my bash script, I ...
1
vote
3answers
68 views

bash + find specific word in space-delimited string

The parameter list include the following example values # echo $list master01.montil131.com worker01.montil131.com worker02.montil131.com In order to find the word that include master string I do ...
0
votes
1answer
77 views

Read command in bash script not executing as a read command and outputting text straight to command line [closed]

I have a bash script that I wrote to automate some commands, and one of the first lines in the script isn't working on the computer that it needs to run on. The code is below #!/bin/bash #some ...
1
vote
1answer
43 views

Bash and Sudo forking continuously, hidden fork bomb?

I am experiencing a frustrating problem where my bash and sudo programs seem to be replicating thousands of processes on Mac. I have searched for all kinds of ways to stop them. I don't know what to ...
5
votes
6answers
889 views

How do I add numbers from two txt files with Bash?

I have a txt file that contains some numbers like this: 1 2 3 4 5 And I have another txt file that contains the same number of lines, but with other numbers: 6 7 8 9 10 I want ...
0
votes
1answer
42 views

bash if -f in home directory

I try to determinate, if a given path points to a file or a directory. I tried this with following code: #if $mypath is a file if [[ -f $mypath ]]; then and #if mypath is a directory if [[ -d $...
0
votes
3answers
49 views

Which quoting style GNU Bash variable definitions (mostly for paths)? [closed]

Which of the following quoting styles, for GNU Bash variables, is preferred and why? Two double quotes: VAR="/path/$V1/path with space/$V2". Multiple double quotes: VAR=/path/"$V1"/"path with space"/"...
0
votes
1answer
40 views

In a bash script, how to use a variable inside a command and assign to a new variable?

I'm trying to store the MD5 of a variable into another variable. Between backticks and the more modern () notation, I cannot figure out how to assign the value of a variable run through a command to ...
10
votes
1answer
435 views

What does the substitution ${!var_name+x} mean?

I found a script that has a function that checks if a variable is set but i don't understand it very well. check_if_variable_is_set() { var_name=$1 if [ -z "${!var_name+x}" ]; then ...
1
vote
0answers
30 views

Make bash understand I want to complete a directory name after “cd”

How to get bash to auto-complete cd commands only with directory names and ignore filenames (just as CMD on Windows) Example: I'm in /some/folder/ I have -rwxr-xr-x filesharp -rwxr-xr-x ...
0
votes
2answers
28 views

How can I jq a line from a file from a remote server?

I would like to check a particular line from a remote server. So far, I can check if the file exists or not. But can I index a particular line using tool like jq? if ssh -q -i $pathToPem $...
0
votes
3answers
70 views

What is this that “ls /d*” returns?

I am running Ubuntu Bionic and exploring Bash Refs. This is an amusing but enigmatic output from ls. Is this the file description of the shell? The command ls /d* outputs autofs disk ...
0
votes
0answers
18 views

Run a function in a Shell Script as a different user [duplicate]

I have a .sh script which contains a set of functions. I have made the script executable (chmod 777). If I want to run a function as my current user I simply do: . /path/to/script/functions.sh &&...
0
votes
0answers
18 views

Do display managers like sddm actually set the login_shell option?

I have a script which distinguishes if it is sourced from within a login shell or not. But it happens not to do its job as the login was done through display manager sddm. #!/bin/bash # /etc/bash....
0
votes
2answers
40 views

How to send the mail's body as fail when the condition fails?

I have an S3 bucket....in which files will be uploaded daily and i should mail them...Mail is working fine...But when the file is not uploaded the mail is sent with empty mail body...How can i put as "...