All Questions
Tagged with bash-functions or bash
27,278 questions
-4
votes
4
answers
97
views
bash pipeline: syntax error near unexpected token 'else'
I'm working on the following pipeline (codeshare.io/243VJE):
#!/bin/bash
#SBATCH --nodes=1 --ntasks=1 --cpus-per-task=10
#SBATCH --time=2-00:00:00
#SBATCH --mem=40gb
#
#SBATCH --job-name=pan_pca
#...
5
votes
3
answers
901
views
POSIX sh alternative to using [[ ... ]] in Bash
I am using this code to parse the first argument passed to my script. It error handles and it works just the way I want it:
if [ -z "$action" ]; then
printf "[${c_RED}ERROR${c_RESET}...
-1
votes
1
answer
2k
views
bash script output not written to file in folder [closed]
My goal is to run a script in crontab and save its output (via echo commands) to a log file. Thus, I am using the following:
$HOME/folder/sh/script.sh 1>$HOME/folder/logs/script.log 2>$HOME/...
4
votes
5
answers
7k
views
Toggle between dvorak and qwerty
I've recently started to learn the dvorak keyboard layout, but I still need to use qwerty sometimes.
I'm looking for a script to toggle between the two keyboard layouts.
Also, I'd like to invoke ...
2
votes
1
answer
2k
views
Command not found when executing in remote server using shell script
I am trying to automate a few steps that I need to execute in a remote linux machine from my local ubuntu computer.
My shell script is as follows :-
#!/bin/sh
ssh XYZ '
qlogin -l cuda=1
'
When I ...
6
votes
2
answers
743
views
Are some bash ambiguous job specs impossible to differentiate?
$ sleep 1234&
$ sleep 12345678&
$ %sleep\ 1234
bash: fg: sleep 1234: ambiguous job spec
$ jobs
[1]- Running sleep 1234 &
[2]+ Running sleep 12345678 &
...
36
votes
4
answers
37k
views
Display file with ANSI colors
I have a file with ANSI colors.
test.txt:
\e[0;31mExample\e[0m
I would like to display the content of this file in a terminal, like cat does, but I would like to display the colors as well.
6
votes
3
answers
577
views
move files to directory according to prefix
I'm in the following situation
> ls
H9/ HG01109_chr1_hap1_contigs.list HG01952_chr1_hap2_contigs.list HG02572/ HG03486_chr1_hap1_contigs.list
...
2
votes
1
answer
217
views
How to run python script on login
I am attempting to load a python script that launches a Firefox Geckodriver under Selenium whenever a specific kiosk user logs in to the GUI.
I've tried launching this via .profile, however ...
0
votes
0
answers
21
views
Launching Browser in Wayland/Cosmic w/ bash script as "non-steam" game in steam doesn't transfer ownership of controller or allow for the overlay
The situation:
I've been converting my family to Linux for quite some time now. I'm currently in the process of replacing their Roku with a Media PC (in this specific case, Pop_OS! 24.04 using Wayland ...
0
votes
0
answers
47
views
bashrc not sourced when running "bash -l", on just one raspberry pi trixie system
Description
I have two Raspberry Pi OS 13 (Trixie) systems with identical Bash versions and identical /bin/bash binaries (verified with md5sum). On one system, running bash -l sources ~/.bashrc as ...
40
votes
7
answers
190k
views
Linux Bash Shell Script Error: cannot execute: required file not found
I have two similar scripts with different names. One works fine but other throws error.
Can anyone please tell me what is the issue?
This is my test.sh scripts which works fine
[nnice@myhost Scripts]$ ...
37
votes
7
answers
20k
views
Running a loop precisely once per second
I'm running this loop to check and print some things every second. However, because the calculations take maybe a few hundred milliseconds, the printed time sometimes skip a second.
Is there any way ...
0
votes
0
answers
43
views
setting and using a shell variable inside "sudo -i bash -c" [duplicate]
I have a little maintenance thing to do as super user, from time to time. I would like that to be a one-liner, for easy copying from my self-documentation to a terminal. I reduced it to the bare ...
2
votes
5
answers
7k
views
How to add an alias without directly editing the `bash_aliases` file?
Is there a way to add an alias from the command line without directly editing the bash_aliases file? To clarify, how can I make a bash script do it without having to enter something along the lines of ...
0
votes
0
answers
36
views
Cannot understand variable assignment logic in bash function [duplicate]
I created this function, which is supposed to allow an interactive selection of an mDNS hostname, and then connect to it via ssh.
Yet, whatever I try, the host variable is only available in side the ...
63
votes
4
answers
55k
views
What is the difference between running "bash script.sh" and "./script.sh"?
If script.sh is just something typical like
#!/bin/bash
echo "Hello World!"
Is there a preferred way to run the script? I think you first have to chmod it so it becomes executable?
2
votes
1
answer
682
views
pwd to return current path with "/" at the end
I want pwd to return /path/to/dir in the format /path/to/dir/ with the "/" at the end. Any ideas as to how I can accomplish this?
25
votes
5
answers
7k
views
Typing a bash command, I'm at its third line. But how do I return to its beginning to edit something I've missed?
I'm typing that command and my cursor is at the end of "supprimer des warnings"
$ git commit -m "Nettoyage :
> - Suppression de sources ou projets inutiles
> - Corrections ...
0
votes
1
answer
71
views
How to move the cursor to an embedded newline on the bash command line? [duplicate]
I have just pasted a line into the bash shell.
It contains embedded newlines.
CTRL+a gets me to https at the start.
CTRL+e gets me to 7d87 at the end.
How can I move around from line 1 to 2 to 3 ...
3
votes
2
answers
350
views
Bash/Dash - Catching the error when calling set
I'm writing a script and I'd like to use set -o pipefail on shells that support it. Dash does not support that option. Usually, || true lets me swallow errors, but not in case of set:
> podman run ...
0
votes
4
answers
6k
views
Effective ACL permissions changing permissions
From a bash shell script, I am creating a folder and storing the mysqldump there. I am sure that there is no command related to permissions in my script. To allow an other user to access these files, ...
4
votes
2
answers
146
views
Manage Gnome GUI via Bash?
I want to power on the computer and have a Bash script to open all my programs in different Gnome work spaces.
For example:
Open text editor and CLI in the first work space.
Open web browser A in ...
1
vote
1
answer
46
views
Cancel just second command of bash logical AND operator [duplicate]
It often happens that I have some long task running, like compiling a program, running tests or copying large archives. Once I am done with whatever else I was doing, I want to leave the computer ...
3
votes
2
answers
8k
views
How to export proxy in Redhat Linux 7 using the current login credentials?
I need to export proxy on RHEL 7 with the current logged user credentials.
I am able to achieve this by adding manually in .bashrc or .bash_profile.:
export http_proxy=http://username:password@proxy....
0
votes
1
answer
2k
views
Bash ignores vi-ins/cmd-mode-string options in .inputrc
When I first created my .inputrc (it didn't exist by default), I set vi-ins-mode-string to "+" and vi-cmd-mode-string to ":" to differentiate between modes. I didn't have "(cmd)" or "(ins)" prepended ...
2
votes
2
answers
3k
views
Self update bash script if there are any updates first then continue on, with Git
I'm trying to add the ability for my ArchLinux installer script to check if it's update-to-date based on rather it matches (or doesn't match) the version number that's on gitlab. The primary script ...
14
votes
3
answers
12k
views
Is there a zsh equivalent of bash builtin readarray?
I recently became aware of readarray in Bash v5. Is there an equivalent for zsh?
0
votes
2
answers
2k
views
/usr/share/bash-completion/bash_completion parse error
I am facing an error when run $ source ~/.bashrc. The error is /usr/share/bash-completion/bash_completion:1512: parse error near `|'.
bash_completion file (the first line is 1512) :
if ! [[ &...
15
votes
2
answers
8k
views
Decode/encode base64url with common unix tools
The common coreutils base64 tool can't encode/decode base64url. The = padding is optional in base64url and the last two characters of its alphabet are - and _.[0][1] What is a simple way to encode and ...
0
votes
1
answer
77
views
disable bash job control warning "job specification requires leading `%'"?
I just rebooted one of my systems for the first time in several months, restarted tmux and my usual set of bash shells for tailing various log files, and noticed that running fg n (i.e. fg followed by ...
2
votes
3
answers
208
views
bash syntax: what does '[@]+' mean? [duplicate]
I have a script that parses command line arguments.
The intro to the loop to iterate over the argument array looks like this:
for arg in ${args[@]+"${args[@]}"}; do
Can someone explain this ...
3
votes
6
answers
433
views
How can I use sed to chain append lines from a text file, add it as a suffix to the text on the same lines numbers on another file, and so on?
I've tried using sed for this. I've tried putting the lines of interest in variables as well.
I have two examples I want to achieve for now. Lets say I have thousands of urls in a file called links....
1
vote
2
answers
663
views
Simple terminal graphics package?
I would like to know if there are any kinds of packages dedicated to ASCII graphics, like drawing lines and plotting points using characters in terminal. I already made a script for plotting points ...
1
vote
1
answer
5k
views
Running Python script over multiple directories and multiple files
Here is my command line for python:
python script.py -a .faa -n .fna
I have a directory containing multiple directories. Out of these multiple directories, there could be 1 or more folders.
In ...
5
votes
2
answers
2k
views
Adding "sudo" commands to the bash history of the *target* user
Something that's bothered me for many years is that if I want to search my shell history for commands which were run as root, I have to check two different locations:
I have to check /root/....
4
votes
1
answer
2k
views
paste: /dev/fd/63: No such file or directory
I'm new to Unix command. I was trying to execute the following commands in Git Bash and it directs to error message:
paste: /dev/fd/63: No such file or directory
# Set Up Image Lists
paste <(awk &...
3
votes
0
answers
840
views
Unable to change default shell to bash on new cachyOS installation [closed]
Updated
Just booted the PC and now it works.
>echo $SHELL
/bin/bash
Very strange. Logging out and back in was not enough.
Just installed new CachyOS linux on new PC (not virtual box)
I looked at ...
0
votes
0
answers
71
views
how to convert hex memory dump to Docsis config file?
Hex memory dump is Docsis config file. Is it possible to convert this hex dump info valid Docsis config file? Preferably, using Perl and DOCSIS::ConfigFile module. (or Python scripts, python-docsis). ...
10
votes
1
answer
10k
views
Case-insensitive completion in bash
I have a folder ~/TestFolder. The current working directory is the home directory(~).
I want to enter a partial command, for example cd test, and I want it auto filled to give the command cd ...
51
votes
5
answers
91k
views
How to get milliseconds since Unix epoch?
I want to do a bash script that measures the launch time of a browser for that I am using an html which gets the time-stamp on-load in milliseconds using JavaScript.
In the shell script just before i ...
8
votes
3
answers
10k
views
One prompt pkexec - two command
Someone could answer me how make one prompt via pkexec when I've to use two command with authentication?
My easy sample script:
pkexec virsh net-start default;
pkexec "/home/user/program";
I'm new in ...
4
votes
1
answer
4k
views
Why is "${ARRAY[@]}" expanded into multiple words, when it's quoted?
I don't understand why "${ARRAY[@]}" gets expanded to multiple words, when it's quoted ("...")?
Take this example:
IFS=":" read -ra ARRAY <<< "foo:bar:baz"
for e in "${ARRAY[@]}"; do echo $...
1
vote
3
answers
5k
views
Unable to run .sh files by double-clicking them
When I try executing a *.sh file, I can run it from terminal, but not when double-clicking it form the file manager. The commands chmod +x Test.sh and
chmod -x Test.sh dont seem to work. Every time I ...
28
votes
7
answers
76k
views
Display Spinner while waiting for some process to finish
How can I show spinner till command line finish it is job? In other words, If I am running a script and I want to show spinner while this script is running and the spinner disappears when the script ...
1
vote
1
answer
117
views
Expected behaviour of GNU parallel --memfree <size> and --memsuspend <size> when size much bigger than RAM
While experimenting with GNU parallel I found that the following cases all hang with decreasing CPU usage on a Fedora 41 VM with 8GB RAM. Is this expected behaviour?
parallel --halt now,fail=1 --...
0
votes
1
answer
2k
views
How can I run the sudo command in Python code under CentOS in Docker
I am trying to access the docker image labels from Python as follows
hostname = socket.gethostname()
cmd = "sudo curl --unix-socket /var/run/docker.sock http:/containers/" + hostname + "/json"
output ...
3
votes
3
answers
917
views
How do I ensure a bash script argument is a specific value?
The following script is supposed to check $1 and see if it is a specific value. It only works when one Bash [[... =~ ...]] regex check is passed in an if statement.
The error is: When more than one ...
1
vote
1
answer
2k
views
Creating Users from a .csv file 2
I am a student and struggle mightily with creating my own scripts. Generally, I can do a bang up job script-kiddie-ing and adding on to what someone else has done following the provided information ...
4
votes
1
answer
223
views
Choice of field separator affects sort's ordering
Suppose we have a script named test_sort in our $PATH with the following contents:
#!/bin/bash
function echo_text () {
printf -- "%s\n" "$fc$oc$fs$lc"
printf -- "%s\n&...