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.

Filter by
Sorted by
Tagged with
0 votes
0 answers
3 views

Repeat test condition for multiple values

I have the following condition, which I want to repeat for three different values for LAB. What can I do? kpn='[^,]+(,[^,]+)*' if [[ "$lab" =~ ^($kpn)?$ ]]; then echo "DN ...
0 votes
1 answer
17 views

Replace backslash path for forward slash path with sed in loop

I want to replace windows server paths (\\100.100.0.0\aaa\bbb\ccc\) with linux paths /foo/bar/ on all occurrences inside all .txt files in a folder (/xxx/yyy/zzz/). For that I made a loop that does ...
0 votes
1 answer
18 views

Grep for a string on remote server returns no results

#!/bin/bash params=$(cat /var/tmp/patterns.txt) remoteLog="/home/remotefile.txt" nodes=("192.168.1.2" "192.168.1.3") for node in "${nodes[@]}";do ...
0 votes
1 answer
58 views

Log from start of script, but decide on log filename during the script

I do not know how to write that part of bash script which enables me to write to the file which is created in the middle of the script (because it depends on user input) but I want to write output ...
0 votes
1 answer
22 views

Back to root_menu in script doesn't respond like it should

Menu and its submenus in single file bash script with a back to root menu for each. My issue is that the show_root_menu call sometimes just jump to the upper menu instead of the root. Bash-installer-...
3 votes
1 answer
352 views

What is "length" of a string in Bourne shell compatibles' `${#string}`?

Arising from this discussion: When I have (zsh 5.8, bash 5.1.0) var="ASCII" echo "${var} has the length ${#var}, and is $(printf "%s" "$var"| wc -c) bytes long" ...
-1 votes
3 answers
58 views

Matching zero or more keywords separated by commas

I want to match the possibility of zero or more keywords separated by commas. Am using the following regex if [[ "$var" =~ ^([^,]+(,[^,]+)*)*$ ]]; then echo "KY MATCH" fi I want ...
0 votes
1 answer
20 views

How to remove all key bindings from tmux session?

I am building a GUI that uses tmux and I want to ensure that all existing bindings are removed when the program is started. I tried using: $ tmux unbind-key -a since the documentation states: If -a ...
0 votes
2 answers
58 views

Get json value using regex in linux bash

I have a json file file.json like this: { "abc": "123", "def": 456, "ghi": 789 } I am trying to get value of all the keys using regex in bash terminal. ...
18 votes
3 answers
24k views

how to extract just the IP address from a DNS query

On Alpine Linux, I'd like to know how to extract just the IP address from a DNS / dig query. The query I'm running looks like this: lab-1:/var/# dig +answer smtp.mydomain.net +short smtp.ggs....
-3 votes
0 answers
35 views

Composing a regex pattern from two short ones

I have a very long regex pattern that I want to split up into two parts. Would the following construct be the correct way to do this? I want to match "## DN [TITLE]" followed by an number ...
0 votes
1 answer
11 views

Neovim open file with search pattern not escaped?

I'm trying to run the command: nvim "./some-file" '+/Text (with/slash)' But I get the following error: Error detected while processing command line: E486: Pattern not found: Text (with And ...
0 votes
0 answers
15 views

bash: adding/removing duplicate programs executes the wrong ones [duplicate]

i'm currently testing the install target (which installs into /usr/local/bin) of some software (that is also installed via my package manager into /usr/bin), and noticed that bash doesn't find the ...
-1 votes
1 answer
33 views

Matching sections of text

I want to do pattern matching to display section of texts within a file matching the following structure. ## DN [Pattern-Matching] glob,regex ## Some text ## More text ## DN [Pattern-Matching] ends ...
0 votes
1 answer
39 views

Why can bash not to follow stty settings?

From an answer to one my previous question I learned that shells (such as bash) have an ability not to follow the rules of terminal input processing set by stty(1). In particular, they can operate in ...
0 votes
1 answer
37 views

Glob and regex matching

Bash has two types of pattern matching, Glob and Regex. The general rule of usage seems to be that 1) the simpler glob is done to search filenames 2) regex is used for searching text. Glob uses the ...
0 votes
0 answers
16 views

issue with python virtualenvwrapper workon

I have installed virtualenv python library to manage my env doing all the setup but when I use the following command to open my env that happen francesco@AirdiFrancesco ~ % workon zsh: ...
0 votes
0 answers
28 views

Is there a way to modify the VENDOR and MODEL fields of removable storage units you can list with the following LINUX lsblk command?

Command: lsblk -l -e7 -o RM,TYPE,PATH,NAME,VENDOR,MODEL |\ awk '{ if ( ($1 == 1) && ($2 == "disk") ) {print $2,$3,$4,$5,$6} }' Context/limitations regarding my question: The LABEL ...
0 votes
0 answers
25 views

Pattern matching using @(pattern) [duplicate]

I would like to study some examples of pattern matching using @(pattern). Have not found actual examples after doing a search.
0 votes
2 answers
27 views

Curl exit immediately if no internet

I want to fetch some apis from github. so this is the code array=(link1 link2 link3 link4) readarray -t item < <(for i in ${array[@]} do curl -s "$i" | jq '.tag_name' done) ...
0 votes
1 answer
37 views

Eliminating ^ and $ in pattern matching

Am using the following regex condition, with ^ and $ to represent the start and end of the given pattern. if [[ "$1" =~ ^[[:digit:]]+$ ]]; then I have also written the equivalent glob ...
2 votes
2 answers
7k views

How to export proxy in Redhat Linux 7 using the current login credentials?

I need to export proxy on RHEL 7 with the current logged user credentials. I am able to achieve this by adding manually in .bashrc or .bash_profile.: export http_proxy=http://username:password@proxy....
3 votes
4 answers
2k views

I keep receiving command not found when I try to run my code in bash

I am trying to write a code that will calculate the radius and area of a circle using pi and the circumference. When I try to run my code, I receive an error message that says main.sh: line 5: bc: ...
-1 votes
4 answers
245 views

How do i filter words starting with uppercase in bash scripting? I have a text.txt , and i want to finde only the words starting with Uppercase (Nnnn)

I want to find just the words starting with a letter in Uppercase. The words must follow this patern. "Nnnnn". I have a file .txt whit a text. I tried using grep but didnt succeed.
2 votes
1 answer
1k views

Bash Error "syntax error: operand expected" but operand existing [closed]

There is a substraction operation that results in an error, however it seems there are two operands given: PREV="$(intranet -r)" PREV=${PREV/Revision: /} ## results in "11072" REV="$(intranet -r)...
0 votes
1 answer
34 views

how to remove  from Shell script output

I'm getting  in my shell script output. can someone please assist as how to remove these  special characters from the output NOTE : I'm storing my output in txt file and sending the output ...
-1 votes
2 answers
42 views

Test for a number in bash [duplicate]

Is this the correct way to test for a number, with double [[]] enclosing :digit: and single quotes surrounding the regex ? if [[ "$var" =~ '^[[:digit:]]+$' ]]; then
4 votes
2 answers
229 views

Test whether a function can be used

Is it possible to test whether a function can be used, in case the file in which the function resides has not been sourced ?
0 votes
2 answers
45 views

How to combine files from incrementing directories into one directory

I have a directory structure that follows this kind of pattern: - Dir 01 (Disc 1) \ - Dir 01 (Disc 1).iso - Dir 01 (Disc 2) \ - Dir 01 (Disc 2).iso - Dir 01 (Disc 3) \ - Dir 01 (Disc 3).iso - ...
0 votes
3 answers
125 views

How can I test if a string has any ASCII white space characters in it?

How can I check if a string has any whitespace characters in it? I do not have to worry about things outside of ASCII for example unicode zero width characters etc. Example of behavior: abc has a ...
0 votes
1 answer
26 views

How do I both change the shell and change the start-up script?-

I am on an embedded distro running with the opkg entware package manager. I have a bash install, and this does not support tab completion now or ever. So, I am switching to zsh. I've successfully ...
0 votes
1 answer
28 views

Conveniently run grep command [duplicate]

Want to be able to conveniently run the following command with ability to supply the search pattern GUNARS. grep -rhi --include='*.el' \"GUNARS\" ~/Opstk/bin/gadin-1.0/" How can I do ...
0 votes
1 answer
45 views

Find and sed not returning output

I am searching for a directory and only getting the last directory back, in my instance xyz. Then, I wanted to use sed to make this happen by replacing all other extensions to that directory. What I ...
3 votes
5 answers
231 views

Replace empty lines in one file with lines from another file

I have file1.csv "word 1" "" "word 3" "" "word 5" "word 6" and file2.csv "replacement text 1" "replacement text 2" &...
40 votes
6 answers
81k views

sftp gives an error: "Received message too long" and what is the reason?

I was able to do sftp yesterday to a RHEL 5.4 box (RedHat) and today I can't. The message is "Received message too long 778199411", and after some investigation, it was due to my RHEL box's ....
0 votes
1 answer
34 views

Extract all content from multiple zip files contained in compressed tar archive

I receive a tar.gz file which packages multiple zip files - reproduced below: $ rm -Rf zipdir; mkdir zipdir; echo foo > zipdir/a.txt; echo bar > zipdir/b.txt; cd zipdir; zip -m a.zip a.txt ; zip ...
0 votes
1 answer
8 views

How to close all tmux windows when one window is detached?

I have the following code: #!/bin/bash tmux new-session \; \ send-keys 'ls' C-m \; \ split-window -h \; \ send-keys 'ls' C-m \; when I run this script, I get two windows. When I want to close ...
0 votes
2 answers
53 views

Adjusting an output of grep in an sh script

Good evening, I'm trying to grep a list of databases stored within a MYSQL dump but I'd like to adjust some of the settings - I'm unsure how to do so. The code is currently; #!/bin/sh echo "What ...
2 votes
2 answers
141 views

Bash command find with exec and grep not working properly, anything wrong?

I want to filter my log files with the keyword ALARM/FATAL in lines into another backup files, suppose I have the following two files [root@TENCENT64 /tmp/test]# cat test_ccd.log ccd0.log:01-28 09:33:...
1 vote
1 answer
37 views

Why does zsh descriptor 10 not have same offset as 0/1/2 when it is connected to same tty? Does that matter?

Why does zsh descriptor 10 not have same offset as 0/1/2 when it is connected to same tty? Would it matter if one were to close fd10 and immediately duplicate fd0 to it? On bash the equivalent 255 has ...
0 votes
0 answers
14 views

Random timeout on basic operation in bash

Context I have a GitHub Action running on a linux machine. It does the following (might want to skip to problem first as most of this might be irrelevant): pwd: /home/runner/work/net.twisterrob....
0 votes
1 answer
20 views

inotifywait script case statement stops working after a while

I have the following bash script, run at system startup to view files dropped into the /srv/mutt directory. It works perfectly for a while but then simply stops 'seeing' files arriving in /srv/mutt. ...
1 vote
3 answers
60 views

Writing bash arguments with trunctation

I want to print the first two arguments of a bash function, with the unicode character \u2263 on each side using a two space separation. The thing is that the final unicode must display at column 70. ...
0 votes
0 answers
16 views

Newlines not getting printed in bash [duplicate]

Why are newlines not printed when calling echo "$var" ? chart () { local var=" find ~/Opstk/bin/gadin-1.0/ -name '*.rc' -exec \ grep -C MYVAR {} \+" echo "$var" ...
0 votes
0 answers
25 views

Colouring a bash print statement

I have the following bash print statement which I want to colour blue. local -r sgr="$( tput sgr0 )" local -r wht="$( tput bold; tput setaf 15 )" local -r blu="$( tput bold; ...
0 votes
0 answers
23 views

Setting option values in bash functions [closed]

What is the most sensible and readable way to specify default values in a bash function - at the start (before testing for option values) or at the end (after testing for option values) ? local cl=...
2 votes
1 answer
33 views

Load variables with source with a single line scope

In bash and other shells, I can make same variables have "line scope" by defining it just before a command. CXX=clang++ ./script.bash Which I prefer over export CXX=clang++ ./script.bash ...
48 votes
5 answers
85k views

How to permanently enable newer version of software installed from SCL repo?

On CentOS 6.4: I installed a newer version of devtoolset (1.1) and was wondering how I would go about permanently setting these to be default. Right now, when I ssh into my server running CentOS 6, I ...
0 votes
1 answer
19 views

Bash script with dynamic number of user inputed variables

I made the following script: #!/bin/bash read var1 ffmpeg -ss $var1 -y -i "$1" -frames:v 1 -q:v 2 "1$.jpg" exit Which works fine for a single file. The scripts waits for input ...
-1 votes
3 answers
133 views

Searching in a CSV for multiple values and get the associated values from it

I have a file.csv "ItemNo","Name","Weight" "a001","Item a","1.1" "a002","Item x","1.2" "a003","...

1
2 3 4 5
508