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
2answers
1k views

chroot with working directory specified

Say I want to write wrapper for chroot command with the possibility to set working directory otherwise preserving chroot semantics. Thus semantics should be: chroot.sh <chroot-dir> <working-...
0
votes
0answers
25 views

Does escaping `bash` aliases make any sense in any - not sourced and normally run - shell scripts?

Does escaping bash aliases make any sense in any - not sourced and normally run - shell scripts? Consider the simplest situation with ls for which many users have re-defined its functioning with an ...
13
votes
5answers
14k views

how to shift array value in bash

we want to build 6 mount point folders as example /data/sdb /data/sdc /data/sdd /data/sde /data/sdf /data/sdg so we wrote this simple bash script using array folder_mount_point_list="sdb sdc sdd ...
0
votes
2answers
62 views

How to disable bash argument splitting?

I want to input any line to my script, and treat it "as is", like a single argument, no splitting by spaces or anything, not removing double spaces. Example: # op.sh IFS=$'\n' echo "$*&...
0
votes
1answer
38 views

compiling bash doesn't work, LFS

I have been following the instructions of the LFS manual so far but have come to an error when making bash, I ran make in the bash-5.1 folder but it displayed an error at the end of the command output:...
1
vote
2answers
71 views

Extracting texts between two markers not always working

So, a while ago I saw this snippet for extracting text between two "markers": # Usage: extract file "opening marker" "closing marker" while IFS=$'\n' read -r line; do ...
1
vote
2answers
64 views

Finding a string in a txt of house addresses with number ranges by passing in exact number and street names

Context I'm a postal worker (mail sorter) and am trying to write a bash script which allows me to input an exact street address and first couple letters of a street name, and have it return the ...
0
votes
0answers
19 views

I need advice on ways to improve my kernel updater script

#!/bin/bash KERNEL_VERSION=$(echo /var/db/pkg/sys-kernel/gentoo-sources* | tr -d -c {0-9.}) cp /usr/src/linux/.config /usr/src/.config-"`date +"%d-%m-%Y"`" cp -avL /usr/src/linux ...
1
vote
1answer
27 views

How can I separate these two columns in this csv file in Linux/Bash?

I am looking to separate these two columns, each into their own separate text files. This data is from a csv file on Kaggle that contains Titanic passenger data. The first column is the number of ...
0
votes
0answers
52 views

How to put an $ variable inside the printing of a command variable? [duplicate]

Source part: if [ $(jq -r '.patch_at' update.json) -ge "4" ]; then recentlycheckedat=$(echo '('`date +"%s.%N"` ' * 1000000)/1' | bc) contents="$(jq '....
0
votes
0answers
16 views

GNU Readline ANSI codes for cursor control in BASH VI mode

While using Bash's vi mode (set editing-mode vi), there is a way to change how the prompt is shown using set vi-ins-mode-string and set vi-cmd-mode-string. Looking for more "visual" methods ...
1
vote
3answers
3k views

bash script not running at startup

I have created a simple bash script called echo.sh. Which outputs a string. I've made sure its executable. And i then added it to the "startup applications" program through the GUI interface. However ...
0
votes
2answers
221 views

Accessing function documentation

Is there a way to access docstrings in Bash? How does one include a docstring in a function's definition in Bash? More specifically how do I add and access docstrings to the following function? ...
3
votes
6answers
520 views

Convert a comma delimited list of numbers to a hyphenated list or range

I have a comma delimited string of numbers as follows: 1,2,3,5,6,7,8,9,12,14 I'm looking for a command to use in a bash script that can combine adjacent numbers into range/hyphenated entries as ...
3
votes
2answers
154 views

Collapsing a series of comma separated numbers in a sequence to Beginning-End [duplicate]

Problem I am trying to solve/enhance a BASH script that provides a number sequence: I am using a topologically aware tool (lstopo-no-graphics) to extract physical processor numbers to use for input to ...
1
vote
1answer
25 views

How to avoid duplicate completion in Bash

I defined the basic completion script. For program d, it can take subcommand exempt, limit, show, and update. complete -W "exempt limit show update" d However, when I press tab after d ...
0
votes
3answers
92 views

Can the words in these bash strings efficiently be inserted into an sqlite table?

I have two bash variables $FNAMES and $LNAMES that I want to insert into a table in sqlite and I'm not quite sure how to do it. Here's a mwe: #!/usr/bin/env sh FNAMES="John Paul George Ringo&...
2
votes
1answer
838 views

Pass script to background using script itself

It is possible to do something like this: nohup ./run.sh > /dev/null 2>&1 & Which sends the process to background, discards its output and allows us to close terminal. Is there any ...
16
votes
5answers
22k views

Recursively add a file to all sub-directories

How do I recursively add(or touch) a file into the current directory, as well as all sub-directories? For example, I would like to turn this directory tree: . ├── 1 │   ├── A │   └── B ├── 2 │   └── ...
1
vote
2answers
2k views

How can I continuously run a bash script checking if a value has changed?

I have a PHP script getAnalytics.php that when called, returns a specific integer value. I want to be able to call getAnalytics.php in a bash script that runs continuously checking the PHP script ...
0
votes
1answer
23 views

sudo crontab notify-send doesn't work

I have read similar posts here, here, here and here that mentioned some environment variables DISPLAY and DBUS_SESSION_BUS_ADDRESS. Setting them at the top of my user crontab enabled notify-send to ...
-1
votes
1answer
29 views

Declare and use ad-hoc function in bash in interactive shell

Basically something like // declare const my_func = function(param1, param2) { do_stuff(param1, param2) } // use my_func('a', 'b'); All in current interactive shell without using files
0
votes
0answers
7 views

URxvt not working properly with backslash characters \n and \t (given my particular ~/.bashrc configuration)

I have the following at the bottom of my ~/.bashrc which shows the last command run and the current working directory at the time of the last command in the title of my terminal window. trap 'echo -ne ...
0
votes
1answer
35 views

How to take command arguments from a file, one per line? [duplicate]

Say I have a file of filenames like this: requests.log responses.log resources used.log and I want to view all of those files. I'd be tempted to do: $ vi `cat /tmp/list` but that winds up opening ...
0
votes
1answer
39 views

What does “[ ”$foo_user“ = 'foo' ] && foo_user='bar' || true” do?

I found this line in one of our repositories without any comments explaining what it does. I am fixing a bug that I found in this repository and don't know what to make of this line. I am unsure of ...
0
votes
2answers
53 views

Special Characters in bash

What is the difference between . (or ..) and ~ character with respect to how they are treated by the bash? If i use ~ character, bash expands it to absolute path but if is use . (or ..) bash does not ...
40
votes
6answers
102k views

Bash: Assign output of pipe to a variable

I am trying to get the output of a pipe into a variable. I tried the following things: echo foo | myvar=$(</dev/stdin) echo foo | myvar=$(cat) echo foo | myvar=$(tee) But $myvar is empty. I don’...
0
votes
1answer
34 views

Cron job not working with alias in bash script

I have been trying to schedule few LTspice simulations on Ubuntu where I simulate using using the following command in the terminal: ltspice stimulus.net (here, ltspice is an alias) I want to run 4 ...
0
votes
3answers
25 views

Get list of packages with version number, already know how to get list without version number

OS : Debian latest. Hello friends, I am working on a small tool, where I am required to send the list of manually installed packages installed for a system as a text file. This text file is then ...
-1
votes
2answers
38 views

comparison to get only lines with changed values on a column

Given the following two csv files, previously sorted: file1 a,1,val1 b,2,val2 c,3,val3 d,4,val4 e,5,val5 f,6,val6 g,7,val7 file2 a,1,val1 b,2,val2 c,3,val3x e,5,val5x g,7,val7 h,8,val8 How can I ...
0
votes
1answer
29 views

Displaying number of columns and items in columns using a for loop

I'm writing a BASH script that asks a user for the number of columns and the number of items to put in the columns. The script will then put the numbers in the columns using for loops. # Number of ...
0
votes
2answers
31 views

How to watch a for-loop in bash?

Is it possible to use a for-loop with the watch command? I'm not really sure what to make of this error with what I've tried: $ for i in 1 2 3; do echo $i; done 1 2 3 $ watch -n 10 for i in 1 2 3; do ...
0
votes
0answers
13 views

Linux: Open a new terminal with command printed but wait for user to execute it

I want to open a terminal with prepopulated command but wait for user to execute it.something like this "gnome-terminal ls" can be helpful here but it executes the command without user ...
0
votes
0answers
4 views

.profile file loaded on starting VSCode

in my .profile i have these two lines daemon plank daemon autokey what these basically do is start plank(dock application) and autokey(custom shortcut maker) on startup, it is working fine but the ...
0
votes
0answers
27 views

Is there a formal metalanguage for the Bash Reference Manual?

Reading through the Bash Reference Manual I sometimes get confused by the notation used to specify the syntax of different elements. It appears that square brackets '[<expression>]' are used to ...
0
votes
0answers
21 views

Something defined a command on macOS

I made a mistake of installing some tool dependent on Go and it somehow redefined my go executable to /usr/local/bin/go from /usr/local/go/bin/go. I deleted the /usr/local/bin/go, cleaned the paths, ...
2
votes
2answers
189 views

Is there an equivalent to vimtutor for bash?

I've "learned" how to use VIM using vimtutor, which I really liked. I think every tutorial should be like this one : "don't try to remember, practice". I was wondering if there was an equivalent to ...
81
votes
4answers
81k views

Exclude one pattern from glob match

I have several files with the same base filename. I'd like to remove all but one foo.org #keep foo.tex #delete foo.fls #delete foo.bib #delete etc If I didn't need to keep one, I know I could use rm ...
1
vote
1answer
37 views

Access different combination of variables in bash

I am new to the group so please apologize for any mistake or badly formulated question. Here's my problem. I have a few arrays of variables. I want to simultaneously start different jobs with all ...
0
votes
0answers
14 views

Strange terminal behaviour when modifing PS1 [duplicate]

Problem Hello! Recently I changed my bash PS1 to make it easy to see my current working directory/username. Unfortunately, this led to the following "features": When nearing the end of the ...
1
vote
1answer
64 views

Use For loop to extract certain columns from a series of files to write new tab-delimited files

I have a series of files located in a series of folder, for example: ~/BR2_1-3/bin.1.permissive.tsv ~/BR2_1-3/bin.2.permissive.tsv ~/BR2_1-3/bin.3.orig.tsv ~/BR2_2-4/bin.1.strict.tsv ~/BR2_2-4/bin.2....
-1
votes
0answers
36 views

Create OpenVPN Alias

The goal is: create an alias that points to the folder that contains the .ovpn files and that already inserts this command "sudo openvpn" and then I just add the name of the file I want to ...
0
votes
3answers
44 views

Trying to read XML attributes from multiple files and replacing them with a number 1.25 times greater than they were before

I am trying to write a bash script or something similar, that I can use to modify all occurrences in a bunch of xml files with one click. I have 24 xml files, each containing a few dozen lines and ...
-1
votes
1answer
32 views

awk in bash cycle

How to apply awk for multiple files, please? The following script does nothing. FILE=/media/linux/*.txt for f in $FILES do awk '{printf("%.4f %8.2f 1.000 1 1\n", $2, $4)}' $f &...
0
votes
1answer
2k views

Setting Colors in ~/.bashrc [CentOS 7]

I'm trying to set colors within my .bashrc. I would like my background to be black, bash prompt text to be white and input text to be lime green. I have this so far: export PS1="[\e[1;37;40m[\u@\W]\$ ...
30
votes
5answers
102k views

How to read from two input files using while loop

I wanted to know if there is any way of reading from two input files in a nested while loop one line at a time. For example, lets say I have two files FileA and FileB. FileA: [jaypal:~/Temp] cat ...
14
votes
2answers
5k views

How do I shift a bash array at some index in the middle?

1 #!/bin/bash 2 # query2.sh 3 4 numbers=(53 8 12 9 784 69 8 7 1) 5 i=4 6 7 echo ${numbers[@]} # <--- this echoes "53 8 12 9 784 69 8 7 1" to stdout. 8 echo ${numbers[i]} # <--- this echoes ...
0
votes
0answers
9 views

gzip by glob with variable [duplicate]

When I use variable within glob expression like gunzip *_{${X}}.gz it does not work. Here is an example: [nir@etl-dev test]$ ll total 96 -rw-rw-r-- 1 nir nir 22431 May 6 10:35 a20210427_AA.gz -rw-rw-...
-4
votes
1answer
101 views

Edit file if present at specific depth on path inside subdirectory tree

A specific file ("target_file") needs editing, if present at depth +2, that is to say if present in any first-level sub-directory with respect to sub-directory path /path/to/alpha/beta. In ...
23
votes
3answers
26k views

Configure up-arrow to browse through commands with same initial characters rather than all previous commands

On the bash command-line, ↑ gives me the previous command. On the command-lines in ipython or matlab, when I type a few characters, ↑ gives me the previously entered command starting with those ...

1
2 3 4 5
440