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.

0
votes
2answers
15 views

Looping over files with specific matching numbers

I'm using a Bash script, and I have two input numbers. For simplicity, let's say the first variable is called Start and the second variable is Stop, and each can be a number between 1000 and 2000. I ...
10
votes
5answers
782 views

Echo with obfuscation

I need to print some variables to the screen but I need to preferebly obfuscate the first few characters and I was wondering if there was an echo command in bash that can obfuscate the first ...
3
votes
3answers
64 views

tee into different variables

From the bash code command1 | tee >(command2) | command3 I want to capture the output of command2 in var2 and the output of command3 in var3. command1 is I/O-bound and the other commands are ...
0
votes
2answers
59 views

Echo Results of a Variable in Sed Replacement

I have a file called LoginExcInternal.txt which has data that looks like the following: Name1 OpenFin/111.111.111.111 Name2 OpenFin/111.111.111.112 Name3 Chrome/111.111.111.111 These two ...
0
votes
3answers
58 views

FOR loop: pass 2 variables from a file simultaniously

I am writing shell script, to extract 2 variables from a file, and have used below FOR loop. for i j in `cat list` do echo title echo total "$i" "," "$...
174
votes
21answers
240k views

How to add a newline to the end of a file?

Using version control systems I get annoyed at the noise when the diff says No newline at end of file. So I was wondering: How to add a newline at the end of a file to get rid of those messages?
0
votes
2answers
36 views

Get return code of first piped command using eval?

I wish to execute different commands and check the return code afterwards before moving to the next steps in the script. At the same time I also wish to log the output of the executed commands to a ...
3
votes
2answers
232 views

Why can't I get pgrep output right to variable on bash script?

I'm trying to make a script to either quit compton if it's running or start it if it's not running. I've read from man that it should exit 1 if process is found, so I've tried to make a script that ...
0
votes
2answers
33 views

new line not append instead append on the end of the line

we gave the following config file ( config.txt ) more config.txt export var1=345476746 but when I am appending new parameter as echo "export var2=5645" >> config.txt we get more config.txt ...
2
votes
1answer
27 views

Can I rearrange the directories in `dirs` in the order of their most recent visits?

What is the order for the pathnames of the directories stored in the stack shown by dirs -l? Are they ordered by their last pushd commands? Is it possible to order them so that they are ordered by ...
0
votes
0answers
25 views

Resource Request! Clear and simple explanation of the syntax and situational use of special characters for Bash scripting [on hold]

When using Bash and the common tools grep, tee, sed, awk and the other common Bash-scripting utilities, I find myself getting confused on the use of special characters such as / \ ' " : ; $ and the ...
0
votes
1answer
26 views

remotely executing a command using ssh

Here I'm trying to remotely execute a command on multiple devices using ssh using while loop in bash however I'm getting output for the first IP in source file but not for others. cat HPE_FRAMES_IP ...
2
votes
2answers
237 views

How can I properly use the 'sed' s command in this particular scenario

I have a names.txt file where each line is of the form: xxxxxx random_string_of_characters 2015 where xxxxxx is a 6-digit number, and random_string_of_characters can be anything. I want to use ...
-2
votes
1answer
88 views

Pass A File Through Shell Script [closed]

I have a script called head.sh, it looks like this: #!/bin/bash { read line1 read line2 read line3 } echo $line1 echo $line2 echo $line3 And I need to call it like this: sh head.sh < ...
2
votes
1answer
2k views

chmod doesn't work on Mac

I cannot make a file executable from the command line on a Mac. This is what I'm typing in terminal cd ./Desktop echo 'say hello' > hello chmod +x hello hello It runs when I double-click on the ...
4
votes
3answers
2k views

Bash: issuing trap twice

If I issue the trap builtin twice [for the same signal], what happens? Is the second command added to the first, or does it replace the first? trap Foo SIGINT ... trap Bar SIGINT ... When SIGINT ...
0
votes
1answer
2k views

Simple question: lftp not writing to $LOG — what am I doing wrong? [closed]

I use a script for work that invokes lftp to mirror a directory: #!/bin/bash HOST='ftp.example.com" USER='pretenduser' PASS='pretendpass' TARGETFOLDER='/home/pretenduser/Dropbox/lftp' SOURCEFOLDER='/...
3
votes
1answer
315 views

Simple Bash regular expression match missing char (OS X)

I am matching a regular expression in bash on OS X 10.11: $ msg='[4] the message' $ [[ $msg =~ (.+)\t(.+) ]] $ echo ${BASH_REMATCH[1]} [4] $ echo ${BASH_REMATCH[2]} he message Between the ']' and ...
1
vote
1answer
69 views

Need a very simple loop for a script

Trying to write a script which uses Charles Karney's GeodSolve to solve a straight-forward geodesic problem. GeodSolve takes the input: lat1 lon1 azi1 dist1, and outputs: lat2 lon2 azi2. I have a ...
6
votes
1answer
271 views

Bash: Why does this Brace Expression work this way?

this is a bash 4 specific question. I am somewhat familiar with the bash Brace Expressions and I have read the manual but I do not understand how it actually works. I guess it has to do with the ...
0
votes
2answers
85 views

Parallelise a for loop in a bash file

I would like to parallelise the for loop in the bash script below. I have two Matlab scripts 1) main.m clear rng default P=2; grid=randn(2,3); jobs=1; 2) f.m sgetasknum_grid=grid(jobs*(...
6
votes
3answers
675 views

Does bash have a color command, as seen in MS-Windows CMD? [duplicate]

There is a command in Microsoft's cmd, called color. I know that, in bash, there are special characters that allows you, during the echos, to change the text colors. As well I do know that in ubuntu ...
0
votes
2answers
150 views

How to use 3rd variable in BASH for loop?

I want to parse data of particular columns from several files(aprilPlate.txt, mayPlate.txt, junePlate.txt, julyPlate.txt, augustPlate.txt) using For loop. The data of input files (aprilPlate.txt, ...
1
vote
1answer
32 views

bash to create sub directories in folder from specific file extension

In the bash below I am trying to create sub-directories inside a directory from files with specific .bam extentions. The .bam file are trimmed and the result is the folder name that is saved in $RDIR ...
0
votes
1answer
67 views

difference between echo ($x) and a=“($x)”; echo $a

When I execute the following in terminal: x=1 a=($x) echo $a #output: 1 echo ($x) #output: -bash: syntax error near unexpected token `$x' Why are above outputs different? Also why are the ...
0
votes
1answer
27 views

Bash script fails where command line works

I wrote a basic bash function to test for the occurence of a string search argument in another string. Running it from the command line gives expected results. Running the same from a shell script ...
0
votes
2answers
35 views

bash remove members Slack from channels using xargs and sleep?

I'd like to be able to remove a batch of people from a "private" Slack channel. Use case: whoops, I just added 137 people to the wrong channel! The Slack Member ID list is obtained from an Airtable ...
-2
votes
0answers
23 views

How to find Elapsed Time or Duration of long running bash jobs [duplicate]

I am trying to capture the elapsed time for long running jobs in Linux. So, I tried to create a small shell to assign variables and create logs Here is my code ### Code Starts Here ### read -p "...
1
vote
1answer
27 views

bash + write standard output and error to both logs

in my bash script I use the following approach in order to write every thing from standard output and standard error to log.txt export LOG=/tmp/installation/log.txt exec > $LOG 2>&1 so ...
0
votes
4answers
45 views

Bash: while loop over numbered variables

My environment can have a flexible number of variables defined with the following format: Var1Value=A Var2Value=B Var3Value=C I now would like to iterate over the variables as long as they exists. ...
0
votes
1answer
26 views

Specific fields from macOS Command “diskutil apfs list”

would appreciate inputs on the subject. I have the output from the macOS (High Sierra;FileSystem :APFS) command - diskutil apfs list. Sample output is given below: +-- Container disk5 1B5FE22B-6F4F-...
3
votes
3answers
5k views

How to preserve the newline character (\n) when capture output of a command in a variable

As a simple example, I have a bunch of source code files. I want to store the "head" command output to a variable for all these files. I tried: output=$(head $file) but what happened is that this ...
2
votes
1answer
34 views

How to make alias interpretive?

I have a list of servers I often connect to, named like server001, server002... server999. I don't want to store 1000 alias hardcoded with these server names. I also don't want to write a function and ...
0
votes
2answers
36 views

Format the output of commands

I am currently trying to use Plink to run a few commands on a linux server to monitor a couple of things on it such as the disk space, memory, cpu usage. I have the commands that I want to use but I ...
56
votes
8answers
161k views

#!/bin/bash - no such file or directory

I've created a bash script but when I try to execute it, I get #!/bin/bash no such file or directory I need to run the command: bash script.sh for it to work. How can I fix this?
1
vote
2answers
897 views

Stop bash script with any key

I'm writing a custom gnome-terminal profile which I want to stop with any key I choose. After pressing the key, my intention is to complete all my commands at that moment and start su. I've tried ...
3
votes
1answer
342 views

What are these three extra chars and why do they cause such a strange error? [duplicate]

Look at these two files: File 1 File 2 They look to me entirely identical, but File 1 has 41 bytes and File 2 has 44 bytes. Now, I would not care about 3 extra chars, however, I think these chars ...
0
votes
1answer
18 views

Get the path of sourced bash script [duplicate]

There is my simple script that I try #!/bin/bash DIR="$(cd "$(dirname $0)" && pwd)" echo $DIR When I execute it like $ ./my_script.sh, get the path correctly. But, when I source it ...
1
vote
1answer
991 views

Bash, find and delete old files

Write script to backup files and delete old backups: #!/bin/sh BACKUPDIR=/var/backups/files ROTATE=1 mkdir -p $BACKUPDIR CURRENT="$BACKUPDIR/`date +%Y-%m-%d`-files.tar.gz" cd /var/ && tar ...
8
votes
2answers
2k views

Indirectly expand variables in shell

I need to indirectly reference a variable in the bash shell. I basically want to what you can do in make by writing $($(var)). I have tried using ${$var} which would be the most straight forward ...
1
vote
3answers
1k views

How to obtain/cat CPU Core Temp from ISA Adapter?

I'm building a script in Linux to monitor my active core temps; Then manually enable the ACPI Fan on the event when the CORE 0 TEMP hits 40 Degrees. I have already built the fan script that works ...
3
votes
4answers
3k views

cpulimit on a bash script which runs other commands

I have a bash script which runs other cpu-intensive commands . When I apply cpulimit on the bash script, the output of top shows processes for the commands inside the script still run without ...
6
votes
2answers
223 views

How to read string as hex number in bash?

I have the bash line: expr substr $SUPERBLOCK 64 8 Which is return to me string line: 00080000 I know that this is, actually, a 0x00080000 in little-endian. Is there a way to create integer-...
2
votes
1answer
50 views

Why am I getting “No such file or directory” when I try to execute a binary? [duplicate]

I'm trying to execute the firefox binary: root@454316a1a89a:/tmp/firefox-portable# ls -la total 220 drwxr-xr-x 4 jenkins jenkins 4096 Mar 18 21:15 . drwxrwxrwt 1 root root 4096 Mar 18 21:...
7
votes
2answers
9k views

Can I automate tar's multi-volume-feature?

Ok, so I've just had a read through this page after a way to improve my current backup solution on my Debian server. Tar seems to be offering a quite nice multi-volume feature, although when I try it ...
33
votes
8answers
147k views

How do I count the number of occurrences of a word in a text file with the command line?

I have a large JSON file that is on one line, and I want to use the command line to be able to count the number of occurrences of a word in the file. How can I do that?
11
votes
2answers
1k views

Exit shell with shortcut (not typing exit) that closes session properly

I use a ProxyJump command for a number of ssh sessions I use daily, and also switch users a lot on these sessions and having to type exit 3 or 4 times in a row isn't too fun. I am aware of newline + ~...
7
votes
4answers
13k views

How to count how many shell/terminals are running?

I´m trying to count how many shell or terminal windows are currently running. I tried ps a|grep bash; ps a|grep tty; But it can be inaccurate.
6
votes
2answers
72 views

Inverting an associative array

Let's say I have an associative array in bash, declare -A hash hash=( ["foo"]=aa ["bar"]=bb ["baz"]=aa ["quux"]=bb ["wibble"]=cc ["wobble"]=aa ) where both keys and values ...
0
votes
1answer
34 views

Preserve presence of last newline when assigning to variable [duplicate]

In bash, when an output is captured into variable using $(...), an annoying newline is appended. However, I have an output that sometimes ends in a newline, and sometimes not. I want the output to be ...