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
4
votes
3answers
297 views

How to have Bash interpret output from command as quoted strings?

I have a program that gets the files selected in the graphical IU (in my case Finder in macOS). The output is something like '/tmp/file number one.txt' '/tmp/file number two.txt' Note the space char ...
0
votes
0answers
21 views

Display file names produced by head to show in red

I am using find together with head to print the first n lines in the terminal. Would like to show the lines containing the filenames in red. Have done an attempt with sed, but having problem with \e. ...
7
votes
2answers
1k views

Why does man complain about a function defined in .profile?

I use Bash 5.1.8. Running man shows the manual page but with the following errors man ps sh: bat: line 10: syntax error near unexpected token `(' sh: bat: line 10: ` *.?(ba)sh)' sh: error importing ...
1
vote
0answers
24 views

Compare version .9 with .10 in bash and find the higher version [duplicate]

I have a bash script for updating a tool which should compare the currently installed version with the latest available version. This always worked until now, where a new version was released, called &...
0
votes
1answer
52 views

How to rename Diectory and Subdirectory with Linux

I would like to remove the "." in the names of my folders and subfolders. I have folders called "Archive.1"; "Archive.2" (etc.) in which I have folders called "...
6
votes
2answers
310 views

How to do formatted printing with jq?

jq has built-in ability to convert numbers to string or concatenate strings. How can I format strings inside jq similar to printf like padding (%4s). For example, how can I force number to occupy 10 ...
6
votes
1answer
128 views

Execute bash function to run screen

I'm using an aws ubuntu instance. I would like to create an alias/function to run some shortcuts, for example to activate a python virtual environnement in a screen. I've made this function for ...
3
votes
2answers
44 views

File Provides Different Mime-types on Different Operating Systems

So I have a bash script that in part does the following: file -b --mime-type "$1" I then have logic to check if it is the expected type of file. if [[ $type == "application/gzip" ]]...
0
votes
1answer
42 views

Bash or condition using variable and numeric value

I have two variables $filetr_exec and $filetr_dryrun. I want to append --dry-run to the array oser whenever filetr_dryrun == 1 or filetr_exec is not set. This means that if both are set, I still ...
0
votes
1answer
26 views

Remote difference between “ip addr del” and “ip link set down”

I am working on a system where a remote SBC monitors the health of all linked servers and issues fault reports when certain behaviors are observed. Everything is running RHEL 7. I have an interface on ...
-1
votes
1answer
50 views

./shell.sh: line 6: [: =~: binary operator expected

I'm trying to execute the following shell script, where I'm trying to keep executing a command in an infinite loop and until the output is not equal to a certain substring checkDeviceStatus=$(adb ...
0
votes
0answers
15 views

why the nohup background process still timeout when execute in Github Actions

Now I am using this script to restart my process: #!/usr/bin/env bash set -u set -e set -x # # const define area # PROGRAM_NAME="schedulespider.py" CELERY_PROGRAM_NAME="celery" ...
0
votes
1answer
41 views

unable to satisfy if condition in shell script

I was trying to execute the following shell script below function Check_Status () { if [[ "$(adb shell getprop sys.boot_completed)" =~ "adb: no devices/emulators found" ]]; ...
0
votes
2answers
67 views

why the pid not split to an array in bash

I want to restart celery from server, because the celery has many process, to I write this script to query all process id and kill it: # # stop celery process # PID=`ps -ef|grep -w ${...
-1
votes
1answer
26 views

how to use echo or printf with xargs to iterate over a list from a file?

In poking about with xargs, as here: nicholas@gondor:~/x$ nicholas@gondor:~/x$ xargs echo < list.txt 1 2 3 nicholas@gondor:~/x$ nicholas@gondor:~/x$ cat list.txt 1 2 3 nicholas@gondor:~/x$ ...
1
vote
0answers
11 views

How to append a variable to CURL command [duplicate]

i am trying to use curl qrenco.de/google.com with an alias so that the alias takes the input parameter google.com and appends it to curl qrenco.de/. i want it to look something like this: $qrcode ...
1
vote
1answer
22 views

how to use xargs to extract a list of names from IRC

Using the finch IRC client, I copy/pasted a list of names to names.txt for processing. Basically, the names are seperated by spaces. Many of the names will have special characters like "/" ...
3
votes
7answers
829 views

Recursively remove extension from all files with specific extension

I have a directory with a bunch of subdirectories with a bunch of files with the extension .no_sub. I want to rename every file with extension .no_sub to the same name with .no_sub removed. So foo....
1
vote
1answer
32 views

Adding directory path to a log file name

Am writing script using bash. I have a name for a log file log and want to prepend a directory path $destin. But $destin can either end with / or without /. log="$destin/$log"
0
votes
1answer
104 views

How to replace `\t` with a TAB character in a string, in a way that is independent of the shell? [duplicate]

I have a script that is going to be executed by sh and bash on Debian Linux. When it is called by sh, the following echo command works perfectly, and replaces \t with 3 spaces. However, it fails when ...
3
votes
1answer
85 views

Preserving double quotes in a command created from variables in shell script

Thought I would write a simple script for synchronizing some data, but it turned out to be more difficult than I thought. The basic layout is that there is a configuration folder, which has subfolders ...
0
votes
0answers
29 views

When you interact with bash (via graphical terminal) what file does bash read from in order to obtain user input? [duplicate]

I'm working with the graphical terminal application on Ubuntu 19.10. This was a homework question and I've tried several Google searches but to no avail. Can someone help me understand the following ...
-1
votes
1answer
30 views

How to keep width format in printf when saving to a variable?

I have the following code printf "%8s.%s\n" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM" "$RANDOM" "$...
1
vote
2answers
71 views

How to store .txt file data in different columns of a CSV file using BASH?

I have a .txt file with the following data structure: Scan Times: 33.3 seconds 77.4 seconds 33.3 seconds 77.4 seconds Check Times: 110.30 seconds 72.99 seconds 72.16 seconds 110.30 seconds ...
13
votes
1answer
925 views

What does !cp:$ do?

I saw this command in the documentation of one of the programs in my work: cp company-sso /usr/local/bin/company-sso ; sudo chmod +x !cp:$ I'm aware that : will stop the next command from being ...
1
vote
1answer
37 views

How can I remove an error from the beginning of my script without setting my variable as null?

I'm running into an issue where my script will only set the integer that I need for the variable $alive if I allow the following error to show. uptime: /dev/ttys001: No such file or directory I've ...
0
votes
1answer
29 views

Why does sourcing a file alter the paths in the exported environment variables?

I have a file .foo.sh that only contain exports, i.e. export foo="../../../../" export bar="a/b/c/d/" export baz="a/b/c/d/e/f" In a script this file is sourced like this ...
0
votes
3answers
59 views

Recursively traverse directories and retrieve last timestamp file

Let's suppose I have the following timestamp like directory tree: root |__ parent1 | |__ 2021 | | |__ 01 | | | |__ 22 | | | | |__ 12H | | |...
0
votes
2answers
27 views

Function for mergin every five .txt files

I have a problem, my folder contains 1500 .txt files. I need to write a function to merge every five into one. Now I'm doing it like this: cat 1.txt 2.txt 3.txt 4.txt 5.txt >> 1a.txt But ...
0
votes
2answers
46 views

Map over file tree, preserving structure

I have a file tree that looks like: $ tree src src ├── bible │   ├── index.md │   └── README.md ├── index.md └── other.md I want to render every Markdown file within this file tree to HTML via pandoc(...
-4
votes
0answers
40 views

How to get processId of a command(running through a Makefile)?

Goal is to send SIGINT signal to a process id Makefile has below task task: pretask docker run --name some-container -d -p 9042:9042 cassandra:latest sleep 20 go run ./cmd/serve/main.go ...
0
votes
0answers
26 views

Why aren't entries in PATH in the order I put them and why does PATH have duplicates?

In my .bashrc I have the following: export TOPDIR=/usr/src/kernels/4.18.0-240.15.1.el8_3.x86_64 export KSRC=/usr/src/kernels/4.18.0-240.15.1.el8_3.x86_64 export PATH=$PATH:/usr/src/kernels/4.18.0-240....
0
votes
1answer
41 views

how to chain script command

Im trying to create an alias that captures my terminal using script when im updating my packages. scripttime() { script --timing=${1-typescript}.time ${1-typescript}.log } alias update-n-log='...
1
vote
1answer
32 views

Assign to associative array element indirectly

When I have the following: a=foo b=baz I can use typeset "$a=$b" But this fails if a references an array element: $ a=foo[bar] $ b=baz $ typeset "$a=$b" $ declare -p foo declare -...
1
vote
0answers
20 views

How to safely remove directory tree in script with path defined by a variable

This is a very basic question, and surely has been asked before, but I am having trouble finding the answer or guidance. There are e.g. these but they don't quite hit the mark: How to safely remove ...
2
votes
0answers
31 views

Looping all .csv files in one directory through a script, and output the results of each loop into a separate .csv in different directory

I am struggling to run multiple .csv files from one directory through a script, and sending the output of each to a different directory. I am able to run the .csv files one by one, but I have 100 ...
0
votes
0answers
14 views

How does makepkg pass variables and functions through fakeroot?

I am at a complete loss on how makepkg passes through variables and functions into the fakeroot environment. I've looked at makepkg and the line that seems to show how it's done makes no sense to me: ...
0
votes
1answer
29 views

openSUSE Tumbleweed: How do I get sudo crontab -u root -e to open the root crontab in nano instead of vi?

Just wondering the above. So far I've tried: export EDITOR=$(which nano) export SUDO_EDITOR=$(which nano) export VISUAL=$(which nano) in both ~/.bashrc and /root/.bashrc, as well as Defaults ...
0
votes
1answer
20 views

How to get the on-screen width of the prompt produced by PS1 (with or without @P)?

Suppose, for example, that PS1 has the value shown below: '\w (\[\e[32m\]${__git_ps1_branch_name}\[\e[0m\] \[\e[31m\]*\[\e[31m\]%\[\e[0m\]>)\$ ' ...where __git_ps1_branch_name is some string, like ...
-1
votes
1answer
109 views

Loop through MAC addresses, how to handle both numbers (0-9) and letters (a-f) in a “for” loop

I've a script with this code for i in {2..9} do grep "Node${i}\|01, source address = 00:00:00:00:00:0${i}" t1.txt > t2.txt done Is it possible to expand the loop ...
0
votes
1answer
37 views

How to change two alternative file extension with a specific extension?

I have two different file extensions abc.fq.gz and abc.fastq.gz and I need to change these to abc.sam. I tried this: #!/bin/bash refdir="Homo_sapiens.GRCh38.dna_sm.primary_assembly" forward=(...
0
votes
1answer
30 views

Get date when the process was started in epoch seconds

I need to get the date when a particular process was started by PID. For this purpose, I use: stat -c %y /proc/PID In the output, I get the date in the following format: 2021-07-04 11:00:20.352976946 ...
1
vote
0answers
22 views

Option+Left in Bash on Mac sometimes produces ;9D

I've set up my bash in iTerm2 on Mac so that Option+Left goes backward a word, and Option+Right goes forward a word. I've done this to have a similar setup as on my Linux machine. However, sometimes ...
-1
votes
0answers
29 views

How do I Multiply and add coulmns then save in another file

I have the below data separated by the following texts interval MGS Q Pro PER CU_AM <=0.3 3.000000e-01 0.000000e+00 0.000000e+00 ...
1
vote
0answers
18 views

python virtualenv activation in bashrc causes environment variables to be overwritten

I have a virtual environment which I usually activate by default in my bashrc via source ~/.venv/env/bin/activate. This has been working fine on an old MacBook Air and Linux Desktops and servers (I ...
0
votes
1answer
37 views

Search for string in most recent file in directory

I wanted to grep a string from the most recent file in the current directory. By using the following command I am able to fetch most recent file but, not able to grep in that file only. $ ls -lrth -...
0
votes
2answers
40 views

Check for line in text bash

This is a really stupid question but I can't find an answer anywhere. So I have a command that prints out lines of text like this: htop kvantum alacritty And I need to check for a line, not a ...
7
votes
3answers
583 views

Is there some interactive analogue of `mktemp` that helps to organize throw-away directories?

I often want a temporary directly where I can unpack some archive (or create a temporary project), see around some files. It is unpredictable in advance for how much time a particular directory may be ...
2
votes
1answer
42 views

Can't access elements of an array built from readarray

I'm trying to build a basic REPL in bash. The script dynamically populates a list of files in a directory for the user to run. File space: | |\ scripts/ || script1.sh || script2.sh | \ shell/ | ...
0
votes
1answer
49 views

AWK subtract 3rd with 1st line, 6th with 4th etc recursively, and then average out

I have a file with a list of numbers, like this 4.90889 4.92681 4.99236 5.29358 5.3115 5.37706 5.9354 5.95332 6.01888 I need to subtract the third number with the first, then procede with the next 3 ...