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.

112
votes
7answers
152k views

What is the difference between “sort -u” and “sort | uniq”?

Everywhere I see someone needing to get a sorted, unique list, they always pipe to sort | uniq. I've never seen any examples where someone uses sort -u instead. Why not? What's the difference, and why ...
0
votes
1answer
28 views

Tab completion after equality sign in bash

How do I get bash to autocomplete when pressing TAB after an equality sign (=)? E.g. starting Abaqus, I use the command line abaqus job= and would like to be able to press TAB after '=' and get a ...
2
votes
4answers
53 views

Delete folders but keep 20 newest, 7 days old

I am looking for a way to delete folders that are older than 7 days, but always have 20 newest folders. similar named folders with _SAME_NAME_@tmp should be deleted as well. I'm stuck with making ...
0
votes
0answers
11 views

Modify nth charter to upper case in a word in a paragraph using bash scripting

A paragraph in a text file, any word that begins with letter t should be re-written with letter T capitalized and any word that has letter h as the second letter of that word should be re-written with ...
0
votes
0answers
16 views

Does “exec bash -l” work from within a script?

As a follow-up question to the answer proposed in Completely restart Bash -- I'm wondering if this works from within a shell script? This is for a setup script for new machines. What I'm doing is ...
0
votes
1answer
27 views

Help with Bash functions

I'm writing a script to process a .csv using it to populate /etc/hosts and create print queues. The bare bones script works however I wanted to build into it some checks to ensure the input is what it ...
0
votes
1answer
20 views

How to read input lines with newline characters from command line?

I found this to get user input from the command line. But it is failing into recognizing the new line characters I put into the input. Doing: #!/bin/bash read -e -p "Multiline input=" variable; ...
0
votes
1answer
19 views

How to close/kill SSH connection when bash shell exits?

We are writing an application. As a part of it, we have to provide SSH access to each clients. In this process, we are providing SSH access to the client using a web UI. This ssh session is invoked ...
6
votes
3answers
6k views

Getting the current path in .desktop EXEC command

Im trying to make a .desktop file run a .sh which is stored in the same directory as the .desktop. The whole directory needs to be portable and moved from machine to machine. my run script is run.sh ...
3
votes
2answers
360 views

Generate collating order of a list of individual characters

In some cases, the collating order of each individual character needs to be known (to be used). It is commonly expressed in the character class of a regex like [b-d]. That character class will match ...
0
votes
1answer
25 views

Terminator layout with command that survives sigkill?

Terminator allows custom layouts with a command such as bash -lc 'npm start'; bash However, triggering SIGKILL (CONTROL-C) will kill part of the terminal and mess up your layout. SIGQUIT works but ...
1
vote
1answer
31 views

Why do I have “command not found” in my script?

I am writing a Nagios plugin and I have a "command not found" when I run it. What is wrong? Why 127.0.0.1 (or localhost as well) is not recognized? When I run just the snmp request - I have a simple ...
0
votes
0answers
8 views

Bash, autocompletion run commands with parameter

I have bash script kbase for 'cat' and 'grep -A10' from specified files for specific word with arguments. Main part of script. cat ~/bash/kb/*.kb | grep "## $1" | grep $2 | grep $3 One .kb file is ...
2
votes
1answer
1k views

Programmatically get CPUs 'detailed' usage, like top/htop

htop has a great way to show CPU usage. It shows the activity (usage in %) of each cores, and each core gauge is divided into three parts : blue: low priority processes green: normal processes red: ...
0
votes
1answer
31 views

Move files to specific folders based on name

I have this file/folder scheme: /Aula /Aula01 /Aula02 aula-01.1.mp4 aula-01.2.mp4 aula-01.3.mp4 aula-02.1.mp4 aula-02.2.mp4 aula-02.3.mp4 All the mp4 files are located in the root directory (...
1
vote
1answer
35 views

BASH execute binary file and read/modify variables

I used to do this: declare -g var='value' . ~script.sh sub and my var was available to read and modify from within my sub. Now I have a binary instead of script.sh and I want the same functionality....
0
votes
1answer
26 views

modify output of command to input as a variable to another

I am trying to modify the output of a command , store it to a variable and pass it to another command in a single line. I tried using xargs, but didnt work well. hre is the first command echo "DIS ...
236
votes
12answers
358k views

How do I loop through only directories in bash?

I have a folder with some directories and some files (some are hidden, beginning with dot). for d in *; do echo $d done will loop through all files, but I want to loop only through directories. How ...
136
votes
3answers
356k views

What is the purpose of .bashrc and how does it work?

I found the .bashrc file and I want to know the purpose/function of it. Also how and when is it used?
10
votes
3answers
5k views

Maintain (or restore) file permissions when replacing file

I have a command that accepts a file as an argument, modifies the file, then writes it to the file name specified in the second argument. I'll call that program modifyfile. I wanted it to work "in ...
3
votes
4answers
1k views

Make many subdirectories at once

Suppose I have a directory /, and it contains many directories /mydir, /hisdir, /herdir, and each of those need to have a similar structure. For each directory in /, there needs to be a directory doc ...
1
vote
2answers
21 views

Pass variable within json payload in shell script

In the following scenario, which is the way to pass ${MY_ENV_VAR} in the payload? I will have to escape: a) the single quotes of the payload b) the double quotes of the value of the text json ...
3
votes
2answers
797 views

unbind default key from inputrc

What I want to do is made C-h not send backwards-delete-char there is nothing about it in my inputrc files but is shows up in bind -p
1
vote
2answers
66 views

Bash: Escaping double quotes in $() command substitution [duplicate]

I am having trouble wrapping my head around how command substitution works when part of the command's parameters come from a variable. To illustrate, I'll just present a series of commands: I'll ...
1
vote
1answer
25 views

Output fdisk bash command to multiline variable

I can't seem to figure out how I can put a fdisk command from a bash script in a multiline variable. Here is my code hdds="$(sudo fdisk -l | grep "Disk /dev/sd" | awk '{print$2}' | sed 's/://g')" ...
0
votes
0answers
29 views

Bash script: if [execution_time] time pass 10 min then write-something else write-somenthing-else [duplicate]

I am a quite beginner with bash scripts - and with Ubuntu as well to be honest. What I would need to do, is the following: Ask for some parameters Loop on other parameters Run a python script that ...
1
vote
1answer
71 views

Prevent dangerous commands from going into bash history [duplicate]

What's the easiest way to prevent dangerous commands matching a pattern (like rm) from going into the Bash command history? I'd like to keep them away from the history of the current session, not just ...
2
votes
3answers
967 views

How to recursively source shell scripts?

This question is, if it matters, preferably in the context of Bash, but I would appreciate a cross-language solution. The problem I'm encountering is in the context of "sourcing" a script with ...
2
votes
3answers
71 views

Generic function to loop over inputs and execute a command in bash?

I was trying to create a function that loops over inputs and executes a command - regardless of how they are delimited. function loop { # Args # 1: Command # 2: Inputs for ...
18
votes
8answers
1k views

Apply brace expansion in “reverse order”

For example {a..c}{1..3} expands to a1 a2 a3 b1 b2 b3 c1 c2 c3. If I wanted to print a1 b1 c1 a2 b2 c2 a3 b3 c3, is there an analogous way to do that? What's the simplest way?
0
votes
3answers
32 views

Question about if statement format

The following code prints "Odd" no matter what number I put in, why doesn't it print "Even" when given an even number as an argument? if [[ 'echo "$1%2" | bc' =~ 0 ]] then echo "Even" else ...
0
votes
1answer
20 views

Bash: Redirect command output to STDOUT and variable

In a Bash script, I need to redirect the output of a command to a variable, while also streaming unbuffered output to the terminal. I've tried this: output=$(command 2>&1 | tee "$(tty)") but ...
1
vote
1answer
34 views

Bash getopts take more positional parameters after the first argument

So I don't know how to make getopts so that it takes every positional parameter after the argument. What I mean by this, is the following: this is my example code: #!/bin/bash while getopts 'a:b:' ...
1
vote
1answer
1k views

Dynamic ~/.ssh/config

I'd like make my ~/.ssh/config file dynamically generated by a shell script (or anything else that prints to STDOUT). Is there a UNIX trick to make reading a file result in executing a command & ...
0
votes
0answers
33 views

Terminal dosen't start and says “bash: warning: shell level (1000) too high, resetting to 1”

My terminal have worked perfectly the last 2 weeks but suddenly after a restart I get an error saying: "bash: warning: shell level (1000) too high, resetting to 1" After 30 seconds it says: "bash: ...
0
votes
0answers
45 views

How to redirect STDOUT from all executables that output more than one screen of text to a pager?

How to stop all executables to dump long output to STDOUT? In other words, I'd like to see the output which goes directly to a terminal to be truncated to the one screen of text or just altogether ...
1
vote
3answers
38 views

How to rename the multiple files within a directory

I want to rename the below files Var1DecoderBase.cpp Var1DecoderDerived.cpp Var1EncoderBase.cpp Var1EncoderDerived.cpp Var1Factory.cpp to Var4Config.cpp Var4CountDownTimer.cpp Var4DecoderBase.cpp ...
8
votes
3answers
22k views

Display Spinner while waiting for some process to finish

How can I show spinner till command line finish it is job? In other words, If I am running a script and I want to show spinner while this script is running and the spinner disappears when the script ...
1
vote
1answer
53 views

bash echo color to textfile [on hold]

How can I write colored text using bash? RED='\033[0;31m' GREEN='\033[0;32m' NC='\033[0m' greprc=$? if [[ $greprc -eq 0 ]] ; then echo -e "${GREEN} converged${NC}" >>log else echo -e "${RED}...
0
votes
2answers
30 views

REGEX: flush a number of related chains in iptables

I want to flush a number of related chains in IPtables. Their names like 'f2b.*' I want the entire chain gone! e.g. all this gone. -A f2b-postfix-sasl -s 103.231.139.130/32 -j REJECT --reject-with ...
0
votes
2answers
32 views

Bash - Find ID Pairs on One File Listed in Another File

I have a large file "F1" with listed id-pairs: id1 = digits, id2 = characters id1 id2 id1 id2 ... The goal is to extract the lines containing the id-pairs from a second file "F2", formatted as ...
0
votes
1answer
37 views

Bash whois script calling local information with *?

I'm getting a very strange output from a script. Here is the line run in the terminal by itself: root@KaliTestBox:~/Desktop/dns1# echo $(whois 13.66.39.88 |head -n 40 |tail -n 28 |tr '\n' ' ') But ...
0
votes
1answer
37 views

Using `read` in bash script from pipeline

Bash can run scripts from pipeline. A minimal example would be: > echo "echo hello world" | bash hello world This is handy if you wants to run a script downloaded from curl or wget. But ...
0
votes
0answers
30 views

Script - Write stdout and stderr to a file AND to the terminal without pipe [duplicate]

I don't know if it's possible, I would like start a bash script without pipe which write everything to a file and at the same time see the output on the terminal. So I know how to do that with this ...
0
votes
2answers
21 views

Alias for recent log files

Usually when I'm searching the logs, its for something that happened recently. I end up tabbing at bash prompt to complete the names. I finally did bash alias that uses date arithmetic to build the ...
2
votes
1answer
3k views

Insert custom XML tag into an XML file in a bash script

I'm trying to create a script using bash to arbitrarily run through a JBoss XML config file and, when I see a specific tag, put some custom values into it. The XML below is the important snippet of ...
0
votes
1answer
31 views

Can someone explain: strstr “\`cat /proc/cmdline\`” rd.live.image does?

Full line of code context: if ! strstr "\`cat /proc/cmdline\`" rd.live.image || [ "\$1" != "start" ]; then exit 0 fi strstr is defined as: strstr() { [ "${1#*$2*}" = "$1" ] && ...
0
votes
1answer
33 views

What does `exec {STDIN}>&0` do? [duplicate]

I saw this in a bash script, and trying to make sense of it: exec {STDIN}>&0 exec {STDOUT}>&1 exec {STDERR}>&2 Are the {STDIN}, {STDOUT} etc "magic strings" recognized by exec ...
0
votes
1answer
30 views

Bash: how to avoid duplicate result from random list?

Suppose a script like this #!/bin/bash array[0]="123" array[1]="333" array[2]="5566" array[3]="135" array[4]="68" array[5]="45" array[6]="78" array[7]="32" array[8]="190" number() { size=${#array[@]}...
29
votes
5answers
65k views

How to use a timer in bash?

I needed a timer which will start at the very beginning of the script and stops at the end.