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
1 vote
1 answer
53 views

How to preserve parameter expansion passed to a function?

I have this function: cyan=`tput setaf 6` reset=`tput sgr0` function Info() { echo "${cyan}$1${reset}" } And I use it in my other scripts as simple as Info some message. However, when I ...
user avatar
1 vote
1 answer
37 views

How to append to an array with a dynamic variable name?

section_example=(one two three) name=example; section_$name+=(four) bash: syntax error near unexpected token `four' Section names are unknown beforehand. eval and declare -a output the same error. ...
user avatar
  • 140
0 votes
1 answer
16 views

Using expect in bash to execute a command with a password request

I have worked for a while on making a BASH script to go through two lists and creating a command out of it. In the end it should execute the command and synchronize my ZFS server and ZFS backup server....
user avatar
0 votes
0 answers
39 views

Alpine ash seems to have trouble with a "while; do ...; done that bash is fine with

I was looking at the following, which I needed: Prepending a timestamp to each line of output from a command . I'm not using the accepted answer, I'm using the POSIX solution described in one of the ...
user avatar
0 votes
0 answers
15 views

Bash's nullglob breaking variable assignments in source'd function definitions

So I've gone a little loopy tracking this problem down. I have a workaround, but it's ugly and I'd appreciate some better solution. There's a Giant Pile of Automatically-Generated Shell Scripts that ...
user avatar
  • 103
1 vote
1 answer
21 views

modify group for one command

I want to execute one command and I want that it is executed with a specific groupname, that I'm belonging to, but which is not my default group. So all files created will belong to that group. Is ...
user avatar
  • 113
0 votes
0 answers
25 views

Read string from file and pass to command, keeping dollar sign intact

Consider a string stored in a file, which I need to read and pass to a Node script via NPM. But the string in the file contains a $, and is being interpreted as a variable and is replaced before being ...
user avatar
  • 243
0 votes
0 answers
12 views

Systemd Service Java/Bash Script

I wish to create a custom service to start/stop a java process. There already exists a start.sh/stop.sh for this java program. sh-4.2$ cat /mnt/crx/publish/start.sh #!/bin/bash if pgrep -x java >/...
user avatar
  • 1
0 votes
1 answer
24 views

String compare negating in bash doesn't behave as it should be?

I have this little bash script: readonly imagick_extension=$(cat "${php_configuration_file}" | grep imagick) echo "Imagick extension grep value: $imagick_extension" if [ "${...
user avatar
  • 103
0 votes
1 answer
40 views

How can I batch rename files by combining three variables and ignore file extensions?

I'm trying to create a script that renames a group of files. Normally they will be TIFs but sometimes an occasional oddball could be thrown in so I would like to rename the file and ignore the file ...
user avatar
0 votes
1 answer
43 views

Change text using bash

I have a program called gamemode, and to check if it's running, you can use gamemode -s, which outputs either "gamemode is active" or "gamemode is inactive". I want to change "...
user avatar
  • 1
1 vote
1 answer
49 views

How to use find and sed to replace certain sentences in files

I am using find and sed commands to find and replaces the occurrences of certain sentences in the whole directory with some other sentences. For example if I want to replace all occurrences of the ...
user avatar
  • 167
1 vote
0 answers
23 views

Change the printed memory usage from dot to comma and then calculate the memory usage in %

So far, in a previously asked question: Calculate the real memory usage (memtotal - memfree - cached - bufferd) with /proc/meminfo one-liner With many thanks to @Philippos this command prints the ...
user avatar
  • 21
1 vote
0 answers
10 views

define hierarchy of history files for ZSH

My zsh syncs history across terminals. Sometimes this is useful. But many times I just need the last command ran on a specific terminal. So I'd like to have two history files in use by each terminal ...
user avatar
1 vote
1 answer
63 views

Extracting text with expressions from a text file using grep

I have the following 2 lines in my text file and wanted to calculate duration between those lines X & Y in minutes. line X: 18.05.2022 13:54:52 [ INFO]: Starting Component 'OWN_FUNDS_RULES' (5/15)...
user avatar
5 votes
2 answers
458 views

Different behavior between SSH into VM and running SSH commands from script

I'm relatively new to shell scripting, so I apologize if this seems like a simple question to ask. I have a linux VM (debian, version 11 (bullseye)) that I can ssh into (ssh <ip>), installed a ...
user avatar
2 votes
1 answer
55 views

Command '“export' not found when starting a terminal app in Ubuntu

Every time I start my terminal I get this message; how to get rid of it? Command '“export' not found, did you mean: command 'mexport' from deb mblaze (0.6-1) Try: sudo apt install <deb name>
user avatar
1 vote
1 answer
36 views

Delete folders from a directory once it's sbatch`ed in a for loop?

I am running a command using sbatch for a tool on the server, that takes a lot of space. The command uses some directories (some files) from a main directory(tq_first) in a for loop. I was wondering ...
user avatar
  • 13
1 vote
1 answer
14 views

Filter rows of a huge csv file based on values in 2 columns

I have a .csv files as shown below: RepDate;FOO1;ProcessId;ClusterName;ProcessType 19700101;-1;Job_1000969943637142044;PROD;auswertung.mpeTestPlayer.xml 20181231;root;Job_107953405843118835;PROD;...
user avatar
0 votes
2 answers
41 views

modify umask of a running process

I have a program, which can be used by a group and thus the output should have group writing permission by default, thus umask 002. Of course each user could do (umask 002 && cmd). But since ...
user avatar
  • 113
0 votes
1 answer
27 views

inotifywait not detecting new directories

The script below watches for close_write events in the TARGET directory and does something with the file #!/bin/bash TARGET=/home/*/ftp/files inotifywait -m -r -e close_write $TARGET | while ...
user avatar
0 votes
1 answer
28 views

How to merge and sort two shell variables, removing duplicates

In my bash script, I'm going to have two variables called "args1" and "args2". Each one of them is going to have content like this: Matching cluster names: [a-z]+ [a-z]+ ... So &...
user avatar
0 votes
0 answers
34 views

change the bash output with stream editing [duplicate]

If I call bash like this: bash|sed 's/a/b/g' every time bash outputs an 'a' it is replaced with a 'b' $ bash|sed 's/a/b/g' $ echo aaa bbb $ exit However if I add additional substitutions like so: ...
user avatar
2 votes
0 answers
39 views

How to find (and move) all duplicate file names

Due to moving files back and forth from my Linux PC to a Mac, I now have a few documents that duplicate. Their names seem identical, but apparently they are codified slightly differently, like in this ...
user avatar
0 votes
2 answers
34 views

Define bash function and chain `script` with it in a shell script

I am having issues defining a bash function inside a bash script and getting to use it in the same script when I try to chaining it after the script command. A minimal working example is this. I have ...
user avatar
  • 103
2 votes
1 answer
137 views

How to properly use quotes for a bash function?

I am trying to define the following bash function in my .bashrc file: function myfind() { find $1 -not -path venv -not -path .tox -name "$2" | xargs grep -n "$3" } which is ...
user avatar
  • 5,160
-1 votes
1 answer
30 views

-bash: syntax error near unexpected token `(' when using lookahead and lookback?

The use case is rather simple. I have a text file, say the following named eg.txt: 'simple_example': 345, 'to_demonstrate': 232, 'regex': 'is not easy to use' I am trying to capture the keys: grep -...
user avatar
  • 101
1 vote
1 answer
26 views

How to automate the process to open 3 shells and run 3 commands?

I set up something so that I'd open up 3 shells and run the programs one by one: cd foo/bar ./foo.sh cd foo/bar node bar.js cd foo/bar ruby foobar.rb However, since it is time consuming, so I wrote ...
user avatar
2 votes
2 answers
40 views

Setting time zone in a kindle bash file

I've recently jailbreaked my kindle to make it a clock that shows a certain image for each minute of the day. I used the instructions mentioned in this article https://www.instructables.com/Literary-...
user avatar
5 votes
4 answers
696 views

Shellcheck approved way to pass 2 arguments in one variable, to a command in a bash script

In a bash script, I call another programme, but I want to configure that programme with a command line option. The following works: AREA_ARG="" if __SOME_SETTING__ ; then AREA_ARG=" --...
user avatar
  • 1,049
0 votes
1 answer
21 views

How can I extract multiple tgz files into seperate folder which have part of file names?

In linux bash shell, my files are listed like below: xyz_a.tgz xyz_b.tgz xyz_c.tgz and I want to extract each file into separate folder, a b c I tried like this: $ for file in xyz_*.tgz ;...
user avatar
  • 3
2 votes
2 answers
81 views

Replace matched regex in file content with variables

I would like to do the following Search file content with pattern (^[ \t]*services:[ \t]*) If found, replace the pattern by a String (in variable) Else, append the String (in variable) at end of file ...
user avatar
  • 123
1 vote
0 answers
86 views

TAB completion not working for mounted partition

I'm used to mount a Windows partition (G) into my Windows Subsystem for Linux (running Ubuntu 20.04) by giving the following commands: sudo mkdir /mnt/g sudo mount -t drvfs G: /mnt/g Unfortunately, ...
user avatar
0 votes
1 answer
24 views

Change prompt colour inside midnight commander?

I use mc on CentOS9. It has its own prompt on the bottom under the panels. I dont know if it is a separate bash or some sort of emulation, but it has default colour while "normal" prompt is ...
user avatar
4 votes
2 answers
502 views

Bash script segmentation fault

My assignment is to write a bash script that reads a directory and returns the file type of each file within, including all subdirectories. Using the find command is not allowed. I've tried to ...
user avatar
2 votes
2 answers
35 views

Remove files except latest one by numeric order

I am trying to remove files within a bash script. I can list the latest file with : ls | grep core | tail -n 1 and rename it like "keep-core-2994-xyz.bin", then remove the rest with: rm -f ...
user avatar
  • 119
2 votes
2 answers
106 views

Replicating floating point arithmetic WITHOUT bc utility

I'm needing to write a script on some embedded Linux systems that don't have bc installed on them and won't be able to have it installed. The script I'm writing is essentially a monitoring script that ...
user avatar
  • 23
0 votes
0 answers
13 views

bash process icon changed since I ran celeste with it

I set a menu entry for launching celeste via bash and now the bash process in the gnome system monitor has celeste's icon even though I already closed the game. Launching another app this way or ...
user avatar
  • 1
-1 votes
0 answers
11 views

How to setup email alerts on centos when openconnect user connect

My current setup i have ocserv openconnect and postfix server running on centos 8 I wan get email alert when openconnect VPN user connected or disconnect .
user avatar
2 votes
2 answers
182 views

How to add a letter in front of file names in a dir when file names have digit(s) at the beginning

I have a bunch of SVG files in a dir. 0.svg 1.svg 2.svg ... 42Group.svg 500px.svg file1.svg file2.svg ... I'd like to add a letter A if a file name starts with a number. For example, I'd like to ...
user avatar
  • 599
-5 votes
1 answer
55 views

What will this command output?

Can someone please explain in detail what the following bash command will output: for i in $(du -kx / | tr -s "\t"""| cut -d""-f2); do echo $i | rev | cut -d"/"-...
user avatar
0 votes
1 answer
30 views

Compare a file with stdout and print out not match line

Compare file.txt cat file.txt apache2 php7.4 mysql-server to the output of dpkg --get-selections Any package that are not matched the content of file.txt, paste them as the argument of "apt ...
user avatar
  • 1
0 votes
0 answers
18 views

How can I redirect functions from a sourced file to individual scripts?

First I tried grepping the file using the matching only option and adjust from there; # REGEX function () { # ((\w|ç|Ç|ñ|Ñ)+\s+\(\)\s+\{\n) blahblah # (.*\n)+ } ...
user avatar
  • 93
0 votes
2 answers
36 views

What's a clean way to run a specific command C for each line L of a given file F and then move every L where C(L) ran unsuccesfully?

Say that https://example.nosuchtld https://example.net https://example.org https://example.willfail is the content of urls.txt. I want to run <command> <url> for every URL/line of urls....
user avatar
-2 votes
0 answers
66 views

A script that terminates all processes owned by the current user, that have used more than 1 minute CPU time

How would I write a script that terminates all processes owned by the current user whose machine time (TIME column in ps output, which I believe is the CPU time) is greater than 1 minute?
user avatar
  • 17
0 votes
0 answers
17 views

Arrange menu horizontally/detect arrow keys

I want to make a small game using bash script, The game, at the beginning provides 4 options for users to choose by using the four arrow keys such left, right, down, up. ex) 1) Join 2) Sign in 3) ...
user avatar
  • 101
0 votes
0 answers
23 views

shared library not found even if "export LD_LIBRARY_PATH" is set

Here comes a strange question. I have a QT App and it requires some libraries. Normally, the following bash script works: #!/bin/bash export LD_LIBRARY_PATH="./libs" export QT_DEBUG_PLUGINS=...
user avatar
1 vote
1 answer
21 views

How can I use the terminal and track what is being changed?

I was wondering if it's possible to use the CLI to track the changes I am making in an app launched through the CLI. For example: Launch CLI. Enter gnome-control-center. Make random changes in the ...
user avatar
  • 768
1 vote
1 answer
41 views

cannot figure out why the file will not copy

I am trying to write a program which i can reuse for copying content into multiple directories. But for the life of it i cannot figure out why the program does not work and is throwing this error. I ...
user avatar
-2 votes
4 answers
123 views

sed or awk: replace only the n-th occurence of a string

How to write a sed (or awk, or both) which will take the following: echo '1 aa 2 2 3 bb 5 bb 2 5' | sed/awk ... And only replace the n-th occurrence of a string? For example the 3rd occurrence of 2 ...
user avatar

1
2 3 4 5
482