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
1 answer
77 views

Extract lines containing a specific pattern for multiple and save the output to individual output files

I have a folder with many .vcf files. I need to extract all the lines that begin with # and save them in a file and then append all the lines where the 12th column starts with 1/1 in the same file. It ...
user avatar
  • 1
5 votes
4 answers
357 views

Convert to uppercase, except for escaped characters

The methods I found break things further down the line by also affecting linebreaks. For example... $ message="First Line\nSecond Line"; $ echo "${message^^}" FIRST LINE\NSECOND ...
user avatar
  • 180
0 votes
1 answer
27 views

reload the i3 conf in a script

i am writing a script to quick change the mod key of i3 conf0="/home/myuser/.config/i3/config" case "${1}" in (alt) key=Mod1 ;; (win) key=Mod4 ;; (*) printf "Invalid ...
user avatar
0 votes
0 answers
23 views

Is there a way to timestamp the history in bash? [duplicate]

By default, history returns a list of commands and index numbers that can be used to rerun them. Is there a way to also add the timestamp to that list?
user avatar
  • 895
1 vote
1 answer
39 views

Inserting a file without altering the folder's modification timestamp?

I have hundreds of folders with modification timestamps I'd like to preserve. Now I need to copy a single file into them. Aside from this way... timestamp=$(stat -c %y /foldername) cp /tmp/file.jpg /...
user avatar
  • 195
0 votes
1 answer
41 views

How to provide condition to the parameter

While providing external parameter values should have before and after hyphen. If hyphen is not there it should reflect the error stating that the hyphen should be needed before and after the text. ...
user avatar
  • 13
0 votes
1 answer
36 views

What is the difference between pipe output to in-place computation? [duplicate]

I am trying to modify my git remote repo address and I got confused over pipe (|). What is the difference between the two and why the first option doesn't work? # Doesn't work git remote get-url ...
user avatar
  • 3
0 votes
1 answer
37 views

What actually happens when I enter the following prompt on the CLI: cat demo.txt >> cat sample.txt?

Let say we have a text file demo.txt that only contains the word 'demo' and a text file sample.txt that only contains the word 'sample'. I understand that cat demo.txt >> sample.txt would append ...
user avatar
0 votes
1 answer
37 views

looping through filename [closed]

I need to use a function that has as input multiple files. These files are related by the filename (e.g. dog1_animal.txt, dog2_animal.txt, dog3_animal.txt, cat1_animal.txt, cat2_animal.txt, ...
user avatar
  • 1
1 vote
1 answer
42 views

Why is the bash double star (globstar) operator often disabled by default?

It seems to be a very useful feature. But I had to learn that it is often disabled by default. I though about enabling it on my system. But there is probably a good reason why it is disabled. Without ...
user avatar
0 votes
1 answer
68 views

What does the "+x" option in "bash +x script.sh" mean?

I know what bash -x does, but what does bash +x do? Googling found no results and the manual also says nothing about it.
user avatar
0 votes
0 answers
41 views

Systemd service not running

I modified this script to help me continuously sync a folder to a remote using rclone. The code for said script is posted below. My issue is that on first run, it seemed to be syncing properly, only I ...
user avatar
  • 101
4 votes
1 answer
272 views

How to unset an invalid environment variable in bash?

I'm trying to add functionality to an existing docker-entrypoint script, the creators of the image decided environment variables would have the form key.subkey=value. I didn't know it was an invalid ...
user avatar
0 votes
3 answers
61 views

bash insert backslash for every slash in string

I have a string that is a path: /tmp/something I need to escape the forward slashes with backslashes: \/tmp\/something How can I do this? Maybe sed? Please point me in the right direction.
user avatar
0 votes
1 answer
40 views

Is it possible to multiple login on the same server with wget?

The aim is to enter different accounts at the same time. With lynx this is possible using files for each account with the -cfg=~/file argument. wget has a .wgetrc file that can be configured with: ...
user avatar
0 votes
1 answer
35 views

trying to add X hours to an arbitrary date in bash

Given a date string like this: 2022-03-28T23:40:49.721Z I am trying to add X hours. the function would look similar to addHours() { dateString=$1 hours=$2 return date -d 'dateString + $...
user avatar
0 votes
1 answer
45 views

How to expand tab-completion for environment variables in bash?

I have an environment variable stored in my .bash_profile called $MODULES: MODULES='/var/www/html/sites/all/modules/ When I am at the bash prompt and I type cp $MOD and then press TAB to complete, ...
user avatar
  • 123
0 votes
2 answers
29 views

How to keep my third column straight if my second one changes

I have this output from a command. CPU_SrcID#0_MC#0_Chan#0_DIMM#0 0 0 A1 CPU_SrcID#0_MC#0_Chan#1_DIMM#0 0 0 A2 CPU_SrcID#0_MC#0_Chan#2_DIMM#0 0 0 A3 CPU_SrcID#0_MC#1_Chan#0_DIMM#0 ...
user avatar
2 votes
2 answers
65 views

Filtering file list to show only files alphabetically after certain file

I have a huge number of images from which I'd like to feed some to ffmpeg time to time. But I only want to feed ones that are alphabetically after certain image (last frame of previous run, name ...
user avatar
  • 23
0 votes
2 answers
23 views

Echo choices to Choice Menu to run program directly from CommandLine

When I run a specific program with choices Do this Do that Do something else And eventual sub-choices Enter website: Enter credentials: I want to do the following echo {choice} | echo {choice2} | ...
user avatar
-1 votes
1 answer
18 views

xargs -I# showing unexpected behavior - why?

I'd like to zip a whole bunch of folders which are in a certain directory, creating one zip file per folder. To do that, I use the following command: $ find -maxdepth 1 -mindepth 1 -type d | xargs -I@ ...
user avatar
  • 2,191
0 votes
1 answer
26 views

bashscript for quick change i3 mod key

i am trying to write a bash script to quick change the $mod key of i3 i moved the "set $mod Mod4" to the end of my conf #!/bin/bash $1=key $alt="alt" $win="win" sed '$d'...
user avatar
1 vote
1 answer
36 views

Is there any way to make autocompletion work for command option names in Bash?

I'm using Bash 4.4. [root@192 ~]# bash --version GNU bash, version 4.4.19(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or ...
user avatar
0 votes
0 answers
24 views

Scheduled cron job not running on MacOS [duplicate]

Here is my crontab. 3 13 * * 1-5 * * * * * /Users/smveerap/cron.sh >/tmp/cronStdout.log 2>/tmp/cronStderr.log 0 12 * * 1-5 * * * * * /opt/homebrew/bin/python3 ~/git/bashFiles/gitUpdate.py >/...
user avatar
0 votes
1 answer
30 views

How I get values from a JSON document in bash?

I want to write a script that finds my unused volumes in AWS. (the 'status' of used volumes is 'in-use'). When I use the aws ec2 describe-volumes --filters Name=status,Values=available I got the next ...
user avatar
-2 votes
2 answers
41 views

Why does echo in Bash modify the content of a independent variable?

I have the following code (minimal example). #!/bin/bash ...
user avatar
  • 192
0 votes
3 answers
35 views

If the value in the first column is the same, compare second column and if the second one is different save the value in the first column in txt file

I try to write script to get the the values from the first column, which are the same, but have a different value in the second column. Does someone know how to do it ? Example: I have this .txt file: ...
user avatar
0 votes
0 answers
60 views

How to use output from grep to rename the folder that contains the file that grep found the wtring in

I am working on a script that extracts log files from a SAN The storage array has multiple "nodes" in a cluster. The diagnostic logs pulled from the array are called an autosupport. The ...
user avatar
0 votes
1 answer
16 views

Installing node 14 on centos 7, how does it work?

The code for installing node 14 on centos7 is: RUN curl -sL https://rpm.nodesource.com/setup_14.x | bash - RUN yum -y install nodejs How does it work? The first command downloads the package. Where ...
user avatar
  • 101
1 vote
0 answers
13 views

Output capturing and make under Cygwin: "Unable to initialize device PRN" [closed]

A minimal example of the problem under Cygwin: A directory with a Makefile: all: @echo "foo" and a test.sh: #!/usr/bin/bash x=$(make) print $x Now bash test.sh gives: Unable to ...
user avatar
  • 151
0 votes
3 answers
56 views

Separate paragraphs using linux

I have a text file that contains connected paragraphs. I need to separate each paragraph with a blank line. Each paragraphs should begin with an >FP0 pattern, however because the paragraphs are ...
user avatar
  • 3
0 votes
2 answers
55 views

iconv cannot convert from ASCII.. why?

I have this file. If I open it in Total Commander with F3 and press S the proper content will be shown. I tried to do the same thing in bash with iconv: iconv -f ASCII -t UTF8 input.txt but I got this:...
user avatar
0 votes
6 answers
102 views

How to cut string on last underscore character?

I have list of files like below: [root@ods1 backup]# ls -l total 93892810 -rw-r----- 1 root root 651248 Feb 17 08:34 abc_def_g_17-02-2022.sql -rw------- 1 root root 665248 Mar 23 08:46 bbc_23-03-...
user avatar
1 vote
2 answers
43 views

How to execute a string of bash command with command substitution

Suppose I have a bash variable like this: tmp1='$(echo foo)' or tmp2='`echo foo`' How to achieve foo as result? I know that removing the command substitution from the string should work, but is ...
user avatar
0 votes
1 answer
28 views

How to store inner output of a nested bash command (within parenthesis) into one file and outer output into another file?

Here's my command: (time ( \ time sleep 1; \ time sleep 2 \ ) 2>&1 | tee inner.txt ) 2>&1 | tee outer.txt cat inner.txt shows: real 0m1.003s user 0m0.001s sys 0m0.002s real ...
user avatar
2 votes
1 answer
189 views

How to pass optional arguments to `time` command [duplicate]

man time shows it has some options, like this, to write just the time output to a file instead of to stderr: -o FILE, --output=FILE So, I try to use it $ time -o out.txt ls -o: command not found ...
user avatar
1 vote
1 answer
45 views

Is there a command to get the current memory address of the current shell script/command?

I wanted to do some protection against the memory dumping when users can recover my shell script's commands using the (delete) tag and then recover it from its memory address. So I thought it could ...
user avatar
  • 111
2 votes
0 answers
17 views

bash: debug (xtrace) target of redirections

Is there a way to show redirections with bash -x? $ bash -exc 'for word in foo bat baz; do echo $word >$word; done' + for word in foo bat baz + echo foo + for word in foo bat baz + echo bat + for ...
user avatar
  • 21
1 vote
1 answer
17 views

Pipefail prevents command substituation from evaluating pgrep?

My goal is to run a pgrep lftp -c command inside a shell script to check if lftp is already running. But the following script never goes beyond "test" output. Only if I remove the pipefail ...
user avatar
0 votes
2 answers
64 views

Understanding functions (Bash)

I see the below function used in a bash script: LOG_FILE="/root/collect.log";\ date_info() { local cmd; cmd=$(basename $0) echo "$(date --rfc-3339=seconds) $cmd.info: $*&...
user avatar
  • 1
0 votes
1 answer
14 views

using heredoc in case statement for remote ssh host

I have a list of ssh commands to run on remote server, so wrote this snippet. #!/usr/bin/env bash echo '' echo "WOULD YOU LIKE TO PERFORM FILES CLEANUP: " read choice case $choice in yes|...
user avatar
  • 1,603
4 votes
1 answer
112 views

Creating folders and subfolders coming from a text file

I have a scenario which will create a new directories coming from a text file. newDirectory.txt /test/test1 /test/test1/test2 I used this code but I tend to get double test1 subfolder while IFS="...
user avatar
  • 41
0 votes
3 answers
35 views

Adding matched string plus additional string after match in same line

I know the title sounds a little confusing, but hopefully my example will clarify my issue. So I have a file with a list of names (names.txt), for example: john david richard I'm trying to use a ...
user avatar
0 votes
3 answers
34 views

Splitting a file into separate files based on a keyword on a particular column of each header line

I would like to create separate files based on the first chromosome mentioned in the header line. There are 24 chromosomes mentioned in the header line and their sequences are mentioned in the next 2 ...
user avatar
-1 votes
0 answers
28 views

Notification email post script execution

Post execution of script notification email should be sent. For example, my script is like the below, my requirement is to notify the status ok/fail to their respective email. eg mail below ok ...
user avatar
  • 2,941
0 votes
2 answers
43 views

sed and variables-substitutions on an custom script

well i ask here because i have around 12 hours trying with examples on internet for put a replace with sed on a custom script. In the file a had a lots of lines but i want to replace values after &...
user avatar
  • 29
0 votes
0 answers
38 views

script ping and traceroute

Creation of a script allowing to record the traces (log) of the ping and traceroute according to a list resulting from a flat file listing the various sites of Reunion and Mayotte. For the need, it is ...
user avatar
2 votes
1 answer
61 views

Wildcard expansion doesn't happen when Bash script invoked from cron under macOS

I have a Mac mini running macOS Monterey, and it has InfluxDB running in a Docker container. I'm trying to set up a nightly cronjob via a shell script I'm calling to back the data up, zip it, and only ...
user avatar
0 votes
1 answer
48 views

how to modify the color of fields of ls -la?

I recently found an example of colored output when using ls: AFAIK that is on MacOS with the BSD implementation of ls. I know how I can change the color of the filenames via LS_COLOR, but I would ...
user avatar
  • 1,066
0 votes
0 answers
19 views

String concatenation failing after personal access token [duplicate]

What specific syntax needs to be changed in the commands below in order for the end result of the commands to produce the expected value of https://ghp_fyrwTOKEN_REDACTEDgugg@github.com/...
user avatar
  • 5,064