All Questions
Tagged with bash-functions or bash
19 questions from the last 30 days
3
votes
3
answers
901
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 ...
5
votes
2
answers
827
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}...
3
votes
6
answers
392
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....
5
votes
2
answers
544
views
Why doesn't the pwd nullary built-in error when provided an argument, whereas the nullary/unary exit built-in does error?
If I enter pwd foo bar at the prompt of the bash shell, the foo bar part of the command line seems to be completely ignored.
If I enter exit 0 foo, I get bash: exit: too many arguments.
So pwd ignored ...
11
votes
1
answer
1k
views
Is there any difference between [[ -n $1 ]] and [[ $1 ]] in bash?
The test [[ -n $1 ]] yields True if the length of string is non-zero.
But I've seen elsewhere and have tried using just [[ $1 ]] without the primary -n and it seems to have the same effect.
Is there ...
2
votes
3
answers
181
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 ...
4
votes
1
answer
196
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&...
1
vote
1
answer
72
views
Bash: only the first long option is being processed
Suppose we have the file ./testing with contents
#!/bin/bash
# Flags
# Source: https://stackoverflow.com/a/7948533/31298396
TEMP=$(getopt -o ''\
--long first,second \
-...
0
votes
2
answers
144
views
Process sed capture group with a bash function before replacement: "sh: 1: <bash function>: not found"
So, I was playing around with this answer, when I found that neither
printf_stdin () {
read input
printf "$input"
}
sed "/lorem ipsum foobar/ {
s/'/'\"'\"'/g
s/\...
2
votes
1
answer
102
views
Bash: function in a script with flags
Using https://stackoverflow.com/a/7948533/31298396, we can implement flags for a script testing as follows:
#!/bin/bash
# Flags
# Source: https://stackoverflow.com/a/7948533/31298396
TEMP=$(getopt ...
0
votes
1
answer
69
views
Bash Script Linux - combines a QUIET function with other functions without hiding some of them
#!/bin/bash
DefaultColor="\033[0m"
GREEN="\033[32m"
RED="\033[31m"
PURPLE="\033[35m"
YELLOW="\033[33m"
CYAN="\033[36m"
CmdOk="\033[...
0
votes
1
answer
41
views
`xargs sh -c` not picking up last argument (vs `for i in $(...)`) [duplicate]
I just wanted to build a simple bash script that inverted a 'hex color code' (made up of 3 pairs of 2-digit hexadecimal numbers, in the format #RRGGBB). In my first code block I attempt to perform ...
1
vote
0
answers
61
views
Running XFCE on Debian Trixie. Bash history is not saving any sudo commands
Bash history works OK for except for sudo commands.
Code from .bashrc follows.
How can I modify the behaviour to include anything starting with sudo?
NOTE. This behaviour is the same with a brand new ...
1
vote
1
answer
44
views
White spacing is causing variable to not contain all text in result
Disclaimer: I am brand new to bash
I'm building a dialog box and trying to get some Docker commands to populate results.
I am using this to return a result, which works for the most part.
result=($(...
0
votes
0
answers
56
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). ...
0
votes
2
answers
48
views
Source file in bash to set prompt with ANSI colors
I have a file named .ps1, and I'm running source .ps1 in bash to try and set my prompt up with color. The contents of the .ps1 file are:
__prompt_command() {
local EXIT="$?"
local ...
0
votes
1
answer
49
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 ...
0
votes
1
answer
27
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 ...
2
votes
1
answer
36
views
Shell bracket list wildcard doesn't work with variable [duplicate]
The following works:
ls {000/487,000/488,000/489,000/490,000/491,000/492}
...many files being listed
But this doesn't. Why ?
LIST=000/487,000/488,000/489,000/490,000/491,000/492
ls {$LIST}
ls: cannot ...