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
20 views

Stopping parameter expansion in write_file content of cloud-init userdata

Background I'm using Terraform and cloud-init to provision an Ubuntu VM. The Terraform template contains an embedded cloud-init user_data section that contains a write_file directive to write a bash ...
0 votes
2 answers
63 views

What mean $ in $PS1 envoriment variable?

For example, if I check $PS1 variable I get it: [root@ENGDADOS ~]# echo $PS1 [\u@\h \W]\$ But if I reenter this exact variable, it changes from # (root) to $ (usually normal user) [root@ENGDADOS ~]# ...
  • 383
0 votes
1 answer
30 views

handling columns with whitespaces

A hashing command gives the hash and the path separated by a whitespace: k3j48fajsdjf4w8fj path to file/file 1.txt I want to create a table where one column is the hash and the other column is the ...
  • 153
0 votes
4 answers
80 views

how to print first word from a string with multiple words without space?

I have a shell script to print appimage filenames inside a folder like this #! /bin/bash Dir="$HOME/Applications/" Dir2="$HOME/Downloads/" cd -P "$Dir" for f in *....
-3 votes
1 answer
49 views

substitute string within a range [duplicate]

Suppose I have text file such as A1 A2 A3 A4 A7 A27 A31 A56 A100 A8 A11 and I want output like this where A1 to A30 shoud be sustituted with A1 and A31 to A100 should be substituted with A2, using ...
  • 17
0 votes
2 answers
33 views

How to list only first directories in zip file?

unzip -l show all directory and sub-directories/files. I'd like to list only the first directory structure insize a zip file, something like -maxdepth 1 of find command. Is there some way to do it ...
  • 383
0 votes
3 answers
78 views

Sorting number ranges from two arrays in bash

I have two arrays with the same number of elements I want to manipulate. They are read from a file into two arrays (odd numbered lines go into array one, even numbered into array 2): arr1=("1&...
  • 1
0 votes
2 answers
39 views

> symbol appearing when interactively defining function in bash

The behaviour of my shell environment changed: Earlier, when pasting a function definition e.g. function exampleFunc { echo hello } to the shell, it would display as formatted and register the ...
  • 383
0 votes
0 answers
29 views

Script for awscli check not working with crontab schedule

I have written a small code snippet to check the aws cli version #!/usr/bin/env bash if [ -e "/usr/local/bin/aws" ]; then myAWS="/usr/local/bin/aws" else ...
-3 votes
1 answer
79 views

How to write a shell script to create the network configuration file

How can I create the below configuration file using shell script? The file should be created automatically using the script, using input from the user. The path where the file is to be created is /etc/...
  • 1
-1 votes
1 answer
29 views

What is the difference betweem ` and ' in bash? [duplicate]

Today I noticed if I run: ldd `which bash` I get the expected output. But when I run ldd 'which bash' I get error ./which bash: No such file or directory. So what is the difference between the two ...
  • 191
0 votes
0 answers
43 views

Bash script won't work if it's run from gnome files

I have the following script #!/bin/bash nohup /home/user/Android/Sdk/emulator/emulator @Pixel_4_API_30 & The script has 755 access and if I try to run it from the Gnome Files option "Run as ...
1 vote
1 answer
28 views

Concatenating string passed into a control sequence

I have this echo invocation that prints a blue bar in a Bash terminal: echo $'\e[48;2;0;0;255m \e[0m' I would like to pass some variables to it, something like (this doesn't work): ...
1 vote
1 answer
89 views

bash if-else statement not working as expected

I have a script in which I check the execution codes (0 assign to SUCCESS and 1 assign to FAILURE). I am using a if-else statement to check whether the execution code is 0 or 1. If it is 0 I want to ...
2 votes
1 answer
37 views

How to read hourly files of a day and merge it?

I have a folder of Netcdf files. These files are six hourly wind files for a month. I want to combine all the files. I don't know how to make a shell script to read the files in order of hour to ...
0 votes
1 answer
31 views

Bash variable with :

I am new to bash and am trying to understand a script I have been passed. Within the script, I have the following variable: site_source="${site_host[$i]}:public_html" Can anyone confirm ...
  • 271
1 vote
1 answer
54 views

Why are bash and git-bash results different?

Newbie trying to learn bash and jq. I'm on a Windows machine so I thought I'd test with git bash - I thought git bash was identical to bash running on Linux. For my test, I'm trying to extract a list ...
  • 119
0 votes
0 answers
21 views

find (fd) execute with custom function and multiple parameters

I'm quite new to bash scripting so excuse the rough script. I'm writing a simple script that goes through all directories that has a .git directory in them, and checks if the worktree is clean or ...
  • 101
-1 votes
1 answer
58 views

Remove duplicate lines without sorting and ignore case

So, let's say I want to remove duplicate lines without sorting, as in this thread, but I also want to remove duplicate lines that aren't case sensitive without converting all the strings to uppercase ...
0 votes
1 answer
48 views

Why isn't my systemd timer executing my sh script?

My youtube.sh file located in /home/deck/Desktop/youtube.sh: #1/bin/env bash pactl -- set-sink-volume 0 100% xdg-open A youtube video link My youtube.timer file located in /etc/systemd/system/youtube....
0 votes
2 answers
68 views

Is it bad practice to use sed to pass a variable through to another script?

There are two shell scripts. One of which is a source file source.sh. This source file will exist for multiple people each with their own directories. name = "foo" The main script main.sh ...
  • 3
0 votes
0 answers
15 views

Execute when dark mode is toogled

I want to create a simple bash file and I want it to run whenever I turn on or off the darkmode. Where and how should I place the file??
  • 140
0 votes
0 answers
27 views

Configuring a service unit to launch a bash script. Why is this failing?

I am asking systemd to supervise and control a process but for some reason, it is failing. Check the output at the end of the question I have a bash script in /usr/sbin/ros_diagnostics.sh that ...
  • 2,250
11 votes
1 answer
881 views

Use the 'cp' command in a Bash script and exclude a specific directory

I need to run this command in a shell script to copy source > destination and exclude a folder. No rsync, tar, find, mv, etc. Exactly this command: cp -var test/!(test2) testbkp In the shell it ...
0 votes
0 answers
95 views

direnv and environment variables

I'm trying to get autoactivation of conda/mamba environments working using direnv but I'm running across a problem that seems to be due to my lack of understanding of how bash scripts work with ...
  • 1
0 votes
1 answer
48 views

Decanting code in $HOME/.bashrc into a function

I have the following code fragment in my $HOME/.bashrc file: export RED='\e[0;31m' export GREEN='\e[0;32m' export YELLOW='\e[0;33m' export BLUE='\e[0;34m' export MAGENTA='\e[0;35m' export CYAN='\e[0;...
  • 483
0 votes
1 answer
40 views

Bash create parameter named array within function

I'm attempting to write a function that writes arrays with a name that's passed in. Given the following bash function: function writeToArray { local name="$1" echo "$name" ...
  • 383
1 vote
0 answers
33 views

Reusing password to unlock KeePassXC database and ask user for new password when adding new KeePassXC entry password

I do have an issue passing on password to keepassxc-cli in bash. I would like it to unlock and ask for adding a password on KeePassXC entry. When I ask user for a password e.g. read for KeePassXC DB ...
  • 4,753
1 vote
1 answer
35 views

Why this systemctl isn't starting?

It's on Debian 11. The file PwOn.service: [Unit] Description=Send a message with a telegram bot After=network.target Wants=network-online.target [Service] Type=simple ExecStart=/usr/bin/...
0 votes
2 answers
44 views

Command substitution not working with environment variable

I have this command which I want to variabilize. Particularly I want the latest argument presence to depend on an environment variable. ansible-playbook --inventory inventories/ssg-dev deploy.yml --...
  • 103
0 votes
0 answers
17 views

Mail remote commands output on failure

I would like to run a command on a remote server and send an email alert (locally) in case of failure. As surprising it is this actually works: ssh -p1222 root@10.4.4.4 'cd /root/chk; md5sum -c /root/...
  • 1
0 votes
0 answers
25 views

bash script to change avatar in Telegram

Can I change avatar picture in Telegram Desktop via bash script? I searched the Telegram folder by the name of the picture file I set previously as avatar but didn't find anything: grep "...
  • 1,235
0 votes
1 answer
39 views

how to close stdout for program from shell?

I mean not to redirect stdout to /dev/null, but block stdout before starting my program, so for example all of my write(1, "dd", 2); will return -1 as if stdout doesn't exists in my device.
0 votes
1 answer
63 views

How to run bash scripts in sequence and in infinite loop?

How do I run /tmp/1.sh /tmp/2.sh /tmp/3.sh /tmp/4.sh and then loop back to run /tmp/1.sh again? preferably in an infinite loop
-1 votes
0 answers
63 views

What is the most POSIX-compliant shell?

Just out of curiosity, I've been on a long unsuccessful quest to finding the absolutely most POSIX compliant shell out there. From what I now know, mrsh is the top candidate. Although yash might also ...
1 vote
2 answers
61 views

Hacking LD_LIBRARY_PATH to use a recent bash from a chroot

I try to get a more up to date version of bash from LinuxMint. I have a chroot with Debian Sid in my box. What I try to do in a bash wrapper script, early in my PATH #!/bin/bash LD_LIBRARY_PATH=/path/...
0 votes
2 answers
60 views

Unix. Run script across multiple dirs on specific files, where pathname has regex

I want to make a bash script (split.sh) that iterates across multiple dirs with same suffix, and then runs a function for specific files within them. I am almost there: #!/bin/bash path="/mypath/...
  • 99
1 vote
1 answer
34 views

Separate variable scope spawning commands under job control

Is is possible in bash - or other sh-derivative shell - to run in the foreground from command-line a list of commands that have their own variable scope (so any values assigned to variables in that ...
0 votes
1 answer
83 views

evaluate a string that is a bash if-statement

My script has a function do_command() that prints a command and then executes it. I want to put my commands in a bash array of strings, and then print _ execute them with this function. But when I try ...
-3 votes
2 answers
102 views

Bash script to calculate within a CSV [closed]

This is my CSV File: Number;Reference;EANNumber;Manufacturer;Price_B2B;Price_B2B_Dis;Price_B2B_DisPer;Price_B2B_DisAmount;Price_B2C_exVAT;Price_B2C_inVAT 2330113;BP3141;1,31304E+11;APC;13;13;0;0;0;0;;;...
2 votes
1 answer
84 views

Simple template engine for bash, that can use csv data?

I'd often like to quickly take some data, and apply it to a template in bash. e.g. imagine doing the following $ seq 1 2 | eztemplate 'this is a {1} test' this is a 1 test this is a 2 test $ ...
  • 1,579
0 votes
1 answer
26 views

Why does the test command apparently choke on a for loop variable?

I want to determine all broken symlinks within a directory. In ignorance of a better solution, I tried this simple bash one-liner (bash version is 4.2.46, which may be worth mentioning because it's a ...
  • 193
0 votes
2 answers
26 views

List files where searched term occurs more than a Threshold

I have a command like rga --files-with-matches --count-matches --sort path -i -e "use cases?" -e "user stor(y|ies)" -e "Technical debt" -e "Code Quality" -e &...
  • 1,996
0 votes
1 answer
37 views

bash: running a sequence of commands in a subshell with fail-on-error

I have a sequence of commands I want to run in such a way that when any command fails, the rest of the commands should no longer be executed. Let's assume the sequence of commands is echo foo false ...
  • 391
0 votes
2 answers
33 views

Can not move searched files to a directory

The output of the following command is: $ rga --heading -l --context 3 --sort path -i -e "use cases?" -e "user stor(y|ies)" -e "Technical debt" Stepanek G. - Software ...
  • 1,996
0 votes
0 answers
54 views

sshpass No such file or directory

need add string to file on remote ssh server without prompting sudo password myuser=testuser $ sshpass -p "$mypass" ssh -t -o StrictHostKeycheking=no $myuser@$myhost "echo $mypass | ...
0 votes
0 answers
21 views

How can I make e2fsck non interactive?

When I run "e2fsck" command through cli it asks for options like asking for abort, force rewrite etc, and the process get completed successfully. But I want to run "e2fsck" command ...
0 votes
0 answers
13 views

How can I can autocomplete for my custom command, based on the name of files in another directory?

This is my requirement. We have a custom Ssh command that uses ssh under the hood, but automatically provides some options based on some conditions. For example, our developers can write Ssh example....
0 votes
2 answers
38 views

Special array from list of items in current dir

I want to extract a specially formatted array from the list of current dir. $ ls Output: fileABC.ext1 filePQR.ext2 dirMNO dirFGH I want only the dir and files of specific extension I want an array ...
  • 140
0 votes
0 answers
46 views

Is there a bash mode that automatically quotes parameter expansion?

I have a script called render.sh that looks like this: #!/usr/bin/env bash if [[ -f "${ENVIRONMENT}.yaml" ]]; then ENV_YAML="-f ${ENVIRONMENT}.yaml" fi set -x helm3 template &...
  • 101