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
3 votes
1 answer
105 views

Cut or Awk to save lines on array in bash

I have a scenario that I have to put lines of a file .txt inside an array, example of file content; type of host | | first host | | | | second host | | | | | | same ip ...
user avatar
-1 votes
1 answer
55 views

How to remove only 1 line with a certain string

Hi i have a question i have searched everywhere but i cant find it. I want to use the sed command to remove a line containing a certain string. In the text file there are more lines with the same ...
user avatar
0 votes
0 answers
41 views

How can I check if my session is the first boot session of the user?

I want to check if the current session is the first time that the user is used. Is it possible to do this in Bash or another language? I’m using Ubuntu 20.04 LTS.
user avatar
  • 3
1 vote
0 answers
24 views

How can read the content of the active window?

I am trying to create a bash script that selects the content of the active window, copies the content to the clipboard, and saves the content to a file. This file then gets read by some text editor, ...
user avatar
0 votes
1 answer
49 views

expand alias commands of .bashrc before it gets executed in terminal

I am a noob at this. alias gc = git commit -m I want: After typing gc in terminal press space and it will turn into git commit -m if i don't want that, press esc and it turn into previous gc ...
user avatar
1 vote
1 answer
47 views

A basic Bash script (to start a GUI program) works partially in cron

I use this script to restart Firefox overnight (to apply the package manager and addon updates): #!/bin/bash killall -s SIGTERM firefox; sleep 15 firefox -P "user" & firefox -P "...
user avatar
0 votes
0 answers
39 views

Replacing less-than sign and file with file content

I have a small executable that takes a file name as input with the less-than sign : small_executable < myfile.txt This works perfectly. But I would like to replace the input with the file content. ...
user avatar
  • 1
0 votes
2 answers
17 views

systemd service file closing socket

I need to run a C++ binary with systemd. When I execute the binary directly or with a bash script it runs fine for days: nohup ./testbinary & ./test.sh test.sh: #!/bin/bash LD_LIBRARY_PATH="...
user avatar
0 votes
0 answers
15 views

Funtoo keychain - how to avoid blocking i3 login?

I'm using Funtoo keychain on Ubuntu 22.04 and I have followed the instructions to add the following to ~/.profile: eval `keychain --eval --agents ssh id_rsa` I'm running the i3 window manager, from ...
user avatar
  • 119
0 votes
1 answer
16 views

Systemd Bash script daemon causes timeout and fails

I have a service file: Memory.service [Unit] Description=Memory Service [Service] ExecStart=%h/opt/bin/Memory --quiet SyslogIdentifier=MemoryService [Install] WantedBy=default.target It starts a ...
user avatar
  • 1,362
1 vote
0 answers
27 views

How to re-enable the `enable` command?

As part of a script to set up a restricted user, I ran the enable -n enable command. For testing purposes I would like to undo that command but I can't find any documentation on how to do so. I know ...
user avatar
  • 111
-1 votes
0 answers
30 views

loop through a list of files and copy them to their folders

I have 10 jvm application logs in gzip format with incrementing number from 1-9. I want to copy them to 9 different directories so I can untar them in their respective folders. What command or script ...
user avatar
0 votes
0 answers
29 views

Why is `read` an executable bash script in PATH? [duplicate]

I just discovered that on my system (Fedora 35), all of Bash's builtin commands also exist as scripts in /usr/bin. For instance: $ which cd /usr/bin/cd $ cat $(which cd) #!/usr/bin/sh builtin cd "...
user avatar
0 votes
2 answers
37 views

How do I handle strange path characters when piping find output?

I had a problem where I wanted to find the length of each path from a find command. My first attempt was to run something like this: find . -exec sh -c "echo {} | wc -c" \; I got this idea ...
user avatar
0 votes
0 answers
12 views

Commands after the snap enable docker command is erroring out in script file

I have a script file as follows. #!/bin/sh sudo snap enable docker sudo docker image ls sudo docker ps -a sudo docker start postgresql-dev sudo docker start dev-pgadmin psql -h 172.17.0.2 -U ...
user avatar
2 votes
1 answer
127 views

Pass text with unbalanced quotes as argument on command line

I want to forward the job output from a job run by a third party scheduler. The scheduler allows me to insert the output into a command, e.g. like so: python script.py --job-output '{joboutput}' Where ...
user avatar
  • 73
2 votes
2 answers
163 views

bash count files in path with wildcard and assign to a variable

What am I missing? I want to find out how many files match a given pattern in a directory and assign it to a variable. If I type it all straight on the command line it works fine ls /backups/system\ ...
user avatar
  • 1,887
0 votes
1 answer
38 views

Passing flags to bash script with case statement - only first flag is set

This might be an obvious error but I'm just not seeing it. I have a case statement which is supposed to enable passing arguments to a script (ex: script.sh --verbose --all). For some reason, this ...
user avatar
  • 77
0 votes
1 answer
55 views

Run bash script in cronjob

i'm trying to automate a script using crontab. I've been searching for some information to help me, but haven't been successful so far. I created the script with the shebang and full paths. #!/bin/...
user avatar
  • 3
0 votes
1 answer
65 views

comparing 2 files unix

I have 2 scripts file.txt and file2.txt file1.txt |col1|col2| |name|mandatory| |age|mandatory| |address|mandatory| |email|mandatory| |country|not-mandatory| file2.txt |col1|col2| |name|gabrielle|...
user avatar
  • 3
0 votes
1 answer
26 views

Remove particular characters in bash

I’ve got a script that pulls an image from a repo, manipulates it, and stores some info. I’m trying to figure out how to strip the tags that append these variables. A variable might be Nginx:23457@...
user avatar
1 vote
1 answer
30 views

bash IPC using named pipes

Consider the below script #!/usr/bin/bash chan=/tmp/pipe.$$ mkfifo $chan { for x in a b c d e do echo $x > $chan done } & for y in 1 2 3 4 5 do x=$(cat $chan) echo &...
user avatar
  • 4,039
-1 votes
1 answer
35 views

multiple conditions in while loop

I have got 2 bash snippets that work perfectly: MODE=a;while [[ !("${MODE}" =~ ^[0-9]+$) ]];do read -p 'mode?' MODE;done and MODE=a;while [[ "${MODE}" != 'b' ]]; do read -p 'mode?'...
user avatar
  • 69
0 votes
0 answers
44 views

File list isn't displayng while using find with -maxdepth 1 to remove subfolders

I am trying to run the following code FILESIZE="+30M" find "$1" -maxdepth 1 -size "$FILESIZE" -type f -exec echo "put {}" \; in order to get a list of file ...
user avatar
  • 39
0 votes
1 answer
83 views

Executing local script on remote server with local input

I have a python script and input data that the python script takes. I want to execute the python script on remote server without copying python script and data to the remote server. I tried the script:...
user avatar
0 votes
1 answer
25 views

How to display the paths to all files that have a size greater than the specified limit

I need to output the paths to all files that have a size greater than the specified limit (declared as a variable). I've done it using find script: FILESIZE = "+10M"; find "$1" -...
user avatar
  • 39
0 votes
2 answers
41 views

How to escape folder name for touch command?

I've created two scripts. The first reads the mtime timestamps of all folders and writes it line by line to time_old.txt: for d in */ ; do time_old=$(stat --format %Y "$d") escaped=$(...
user avatar
  • 195
0 votes
1 answer
35 views

How to increment a persistant, inter-process counter in bash/shell

Here is a minimal example of what I am trying to do: # If it does not exist, create a file to persist the unique identifier if [ ! -f ~/.uid ]; then echo 0 > ~/.uid fi # Increment the unique ...
user avatar
0 votes
1 answer
23 views

FreeBSD - get timezone in Continent/Region format

I have a Linux (bash) script to generate a login message (motd). One of the things it displays is the timezone, using the command timedatectl | grep Time | awk {'print $3'}. The command returns ...
user avatar
  • 77
-1 votes
1 answer
27 views

In Bash why echo hello | read myvar && echo $myvar doesn't output? Zsh works fine [duplicate]

I quite don't understand why Bash would not hold the value of myvar in this example. echo hello | read myvar && echo $myvar I would understand if I were running that command as two separate ...
user avatar
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
  • 885
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
41 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
66 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
59 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
42 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