Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
47
votes
4
answers
32592
views
Bash: slice of positional parameters
How can I get a slice of `$@` in Bash without first having to copy all positional parameters to another array like this? argv=( "$@" ) echo "${argv[@]:2}";
How can I get a slice of
$@
in Bash without first having to copy all positional parameters to another array like this?
argv=( "$@" )
echo "${argv[@]:2}";
n.r.
(2263 rep)
Jul 7, 2013, 01:12 AM
• Last activity: Oct 29, 2024, 06:25 PM
0
votes
1
answers
51
views
Can we pass directory list from find command as argument to another find command and execute md5sum?
I want to list all the directories in present directory and pass those directories as a argument to another find command The below example will explain it correctly. ``` $ find . -maxdepth 1 -type d . ./dir1 ./dir2 ./dir3 ./dir4 $ echo -e "\n" ; find (above directories as argument) -exec md5sum {} +...
I want to list all the directories in present directory and pass those directories as a argument to another find command
The below example will explain it correctly.
$ find . -maxdepth 1 -type d
.
./dir1
./dir2
./dir3
./dir4
$ echo -e "\n" ; find (above directories as argument) -exec md5sum {} +
So that I get checksum values for every files in a folder separately, separated by space.
I think the above example explains the problem correctly Sorry for my bad English and I have very little knowledge on Linux command please forgive me
Thank you in advance.
user27072144
(1 rep)
Oct 5, 2024, 09:58 AM
• Last activity: Oct 5, 2024, 01:28 PM
3
votes
1
answers
235
views
Convention of parameter naming - undocumented options
GNU has long pushed for a combination of short options (`-h`) and long options (`--help`), which goes a long way to clearing up the syntactical mess of commands like `tar` and `dd`, with their legacy syntax going back to the stone age. But I recently [learned][1] that GNU's `parted` has a _third_ ki...
GNU has long pushed for a combination of short options (
-h
) and long options (--help
), which goes a long way to clearing up the syntactical mess of commands like tar
and dd
, with their legacy syntax going back to the stone age.
But I recently learned that GNU's parted
has a _third_ kind of option, where an 'undocumented feature' can be accessed with ---pretend-input-tty
. It looks like it was introduced in this commit in 2007.
It's the first time I've seen that, and I'm intrigued. Is this some kind of wider convention, or just a one-off?
Popup
(704 rep)
Jun 21, 2024, 06:53 AM
• Last activity: Jun 21, 2024, 09:18 AM
5
votes
1
answers
557
views
Grub.conf vs sysctl.conf
Both files are being used to pass parameters to the kernel. so what exacly is the difference between the two? What is the purpose of each file?
Both files are being used to pass parameters to the kernel. so what exacly is the difference between the two? What is the purpose of each file?
Liron Cohen
(255 rep)
Sep 26, 2016, 04:13 PM
• Last activity: Apr 24, 2024, 01:28 PM
-1
votes
1
answers
77
views
Passing math equations as parameters to a looping function
I want to run several jobs in parallel using the a script that does serial calls to a parameterized script and submits it as a background job on each call. It's for a brute force script which on each call will run 1k of 10k total combinations. The following is my first hack at it. #!/bin/bash for i...
I want to run several jobs in parallel using the a script that does serial calls to a parameterized script and submits it as a background job on each call. It's for a brute force script which on each call will run 1k of 10k total combinations.
The following is my first hack at it.
#!/bin/bash
for i in $(seq 10)
do
./tmp/tmpsky/bd25Par.sh (i*1000-1000) (i*1000) &
done
I want $1 to evaluate as 0,1000,2000 etc...and $2 to evaluate to 1000,2000,3000 etc.
The & is to submit the script as a background job. The called module will pass $1 and $2 to be used with seq as follows
#/bin/bash/
for n in $(seq $1 1 $2)
do
...`
The first script fails with
unexpected token 'i*1000-1000'
What have I done wrong?
Info5ek
(503 rep)
Sep 28, 2016, 04:06 AM
• Last activity: Mar 22, 2024, 05:46 PM
0
votes
1
answers
86
views
What do (G) and (S) mean in samba's parameter configuration?
In the official documentation of [Samba](https://www.samba.org/), the [smb.conf(5)](https://www.samba.org/samba/docs/current/man-html/smb.conf.5.html) page has an "Explanation Of Each Parameter" section. For some parameters, two possible values (either `(G)` or `(S)`) are shown at the end of the nam...
In the official documentation of [Samba](https://www.samba.org/) , the [smb.conf(5)](https://www.samba.org/samba/docs/current/man-html/smb.conf.5.html) page has an "Explanation Of Each Parameter" section.
For some parameters, two possible values (either
(G)
or (S)
) are shown at the end of the name, but nothing for others.
What do (G)
and (S)
mean in samba's parameter configuration? (and _empty/nothing_?)
Manuel Jordan
(2108 rep)
Mar 21, 2024, 11:06 PM
• Last activity: Mar 22, 2024, 02:32 AM
0
votes
1
answers
111
views
how to configure sudo to allow certain parameter
In particular I want to allow the following command without password: ```zsh sudo cfgutil --ecid "${Device_ECID}" install-application "${in_File_Name}" ``` With `Device_ECID` and `in_File_Name` changing from one call to the next. I found several examples but most about about the opposite: prohibitio...
In particular I want to allow the following command without password:
sudo cfgutil --ecid "${Device_ECID}" install-application "${in_File_Name}"
With Device_ECID
and in_File_Name
changing from one call to the next. I found several examples but most about about the opposite: prohibition variable parameters entirely or allowing any parameters.
-----
**Update:**
Shortly after posting I found out about wildcards. The following works for me:
%_developer ALL=(root) NOPASSWD: /usr/local/bin/cfgutil list-devices
%_developer ALL=(root) NOPASSWD: /usr/local/bin/cfgutil --ecid * install-application *
However, from @marcus-müller answer I take it that this is only the case because there are no actual spaces in the filename. So I wonder is there is something else I'm still missing.
Martin
(113 rep)
Mar 21, 2024, 09:51 AM
• Last activity: Mar 21, 2024, 10:24 AM
1
votes
1
answers
399
views
Bash parameter substitution with multi line error message
I'm using parameter substitution with an error message, such as ```${var1:?'some message'}```. I've incorporated a multi-line error message. Presently, it only functions properly when enclosed within single quotes, with line breaks inserted using the Enter key. Is there a clever way to allow accepta...
I'm using parameter substitution with an error message, such as
${var1:?'some message'}
. I've incorporated a multi-line error message. Presently, it only functions properly when enclosed within single quotes, with line breaks inserted using the Enter key. Is there a clever way to allow acceptance of multiple lines, saved separately?
I'm just curious about exploring this. Please avoid suggesting alternative syntax involving if statements unless it's directly relevant.
**Cases:**
works: single quote
needed=${1:?'first error line
second error line
third error line'}
not working: call another string variable
usage_message='Please use this script by providing the following arguments:
1:
2:
3: '
username=${1:?$usage_message}
run_script_path=${2:?$usage_message}
where_to_save=${3:?$usage_message}
not working: call a function
function get_message {
echo -e "first line \nsecond line\nthird line"
# or
# printf "first line \nsecond line\nthird line"
}
needed=${1:? $(get_message)}
Other discussion about parameter substitution:
https://stackoverflow.com/a/77772942/13413319
programmer Erfan
(13 rep)
Feb 20, 2024, 01:37 AM
• Last activity: Feb 20, 2024, 03:25 PM
1
votes
3
answers
910
views
How to make alias trailing with space hack works with parameter supplied
I have two aliases `watchExpand` and `l`. I know that you can make bash expansion work with aliases by placing a trailing space like so: alias watchExpand='watch ' `l` is aliased to `ls -larthiF --context`. So when I type the command `watchExpand l` it works like a charm. However if I supply a param...
I have two aliases
watchExpand
and l
. I know that you can make bash expansion work with aliases by placing a trailing space like so:
alias watchExpand='watch '
l
is aliased to ls -larthiF --context
. So when I type the command watchExpand l
it works like a charm.
However if I supply a parameter to watchExpand
command, e.g.
watchExpand -n 1 l
My l
alias no longer works. How can I get bash expansion after arguments?
林果皞
(5546 rep)
Oct 21, 2016, 05:47 PM
• Last activity: Feb 15, 2024, 10:56 AM
15
votes
3
answers
9769
views
Why $0 is not a positional parameter?
I have read that positional parameters start at `$1` (for example: `$1`, `$2`, `$3` and so on are positional parameters). But `$0` is not a positional parameter. But why `$0` is not a positional parameter? I think this could be a reason, but not sure: Positional parameters only take their values whe...
I have read that positional parameters start at
$1
(for example: $1
, $2
, $3
and so on are positional parameters). But $0
is not a positional parameter.
But why $0
is not a positional parameter?
I think this could be a reason, but not sure:
Positional parameters only take their values when a script is executed. For example: if we do ./myScript Hello
, then $1
will have the value Hello
. But $0
can take its value on two occasions: when a script is executed (it would have the value of the script name), and when bash
itself is executed without a script (it would have the value bash
or -bash
).
Steve
(897 rep)
Dec 23, 2017, 06:35 PM
• Last activity: Jan 19, 2024, 11:15 AM
2
votes
2
answers
355
views
Parameter expansion of variable containing control characters
I'm looking to replace the group separator control character, which is printed by the terminal as "^]" and has got octal code of: 035, with a space. This can simply be done using tr, like so: `echo $line | tr '\035' ' '`. However, I don't (can't) use an external command in my bash script, so am inst...
I'm looking to replace the group separator control character, which is printed by the terminal as "^]" and has got octal code of: 035, with a space.
This can simply be done using tr, like so:
echo $line | tr '\035' ' '
.
However, I don't (can't) use an external command in my bash script, so am instead looking to use parameter expansion to replace the control characters with spaces.
What is the correct syntax to match the control character, the below options don't work:
${line//'\035'/' '}
${line//'\0035'/' '}
${line//\035/' '}
${line//$'\035'/' '}
Or is there another way to replace the character without using external commands such as tr and sed?
Maikol
(164 rep)
Jan 4, 2024, 09:19 PM
• Last activity: Jan 5, 2024, 09:42 AM
0
votes
4
answers
239
views
How can I use parameter expansion together with command substitution?
- I want to create 12 directories named after the months. So I tried using `locale` to get the month names, piping to `tr` to separate them with commas, then put braces around and passed to `mkdir`. But the whole is treated as one string. Is there a way to avoid this? ``` 16:07: temp ⧲ mkdir {$(loca...
- I want to create 12 directories named after the months. So I tried using
locale
to get the month names, piping to tr
to separate them with commas, then put braces around and passed to mkdir
. But the whole is treated as one string. Is there a way to avoid this?
16:07: temp ⧲ mkdir {$(locale mon | tr \; ,)}
16:18: temp ⧲ ls -lh
total 4.0K
drwxr-xr-x. 2 john john 4.0K Dec 11 16:18 {January,February,March,April,May,June,July,August,September,October,November,December}
16:18: temp ⧲
teraspora
(111 rep)
Dec 11, 2023, 04:20 PM
• Last activity: Dec 11, 2023, 10:16 PM
1
votes
1
answers
1373
views
Can I configure parameters when launching flatpak apps from the GUI?
One of my Flatpak application stopped working properly, probably after a silent upgrade. I found a fix online that tells me to add a parameter. That fix works. Nonetheless, I like running that app from the GUI as it's much less time consuming than typing its full name in the console. For a concrete...
One of my Flatpak application stopped working properly, probably after a silent upgrade. I found a fix online that tells me to add a parameter. That fix works. Nonetheless, I like running that app from the GUI as it's much less time consuming than typing its full name in the console.
For a concrete example, my program is VSCodium, installed with Flatpak. The fix is to run it with GPU disabled.
Currently, the GUI runs the command
flatpak run com.vscodium.codium
with probably a few more parameters, I would like it to run flatpak run com.vscodium.codium --disable-gpu
, keeping those extra parameters.
If it were an app installed through the native package manager, I'd modify the .desktop
shortcut, but I can't find the ones created by flatpak, if any.
How do I achieve that? (Where are the .desktop
files or is there a better way?)
SteeveDroz
(203 rep)
Oct 31, 2023, 05:33 AM
• Last activity: Nov 17, 2023, 10:22 AM
19
votes
3
answers
74578
views
Passing arguments from a file to a bash script
I've got four files: ```text ./ ./myscript.sh ./arguments.txt ./test.sh ``` Inside `myscript.sh`, I have to run the file `test.sh`, passing to it the arguments contained inside `arguments.txt`. `myscript.sh` is: ```lang-bash arguments=$(cat arguments.txt) source test.sh $arguments ``` This works wel...
I've got four files:
./
./myscript.sh
./arguments.txt
./test.sh
Inside myscript.sh
, I have to run the file test.sh
, passing to it the arguments contained inside arguments.txt
.
myscript.sh
is:
-bash
arguments=$(cat arguments.txt)
source test.sh $arguments
This works well *if arguments.txt
contains at most one* argument:
firstargument
The substitution is:
-shellsession
++ source test.sh 'firstargument'
But the problem is with two or more arguments. It does this:
-shellsession
++ source test.sh 'firstargument secondargument'
Also, I don't know in advance the number of arguments inside arguments.txt
. There can be zero or more.
Federico Ponzi
(744 rep)
Jul 21, 2014, 05:35 PM
• Last activity: Oct 6, 2023, 02:50 PM
143
votes
6
answers
177967
views
How to pass parameters to an alias?
For bash script, I can use `"$@"` to access arguments. What's the equivalent when I use an alias?
For bash script, I can use
"$@"
to access arguments. What's the equivalent when I use an alias?
prosseek
(8748 rep)
Nov 4, 2010, 09:44 PM
• Last activity: Sep 15, 2023, 03:57 PM
0
votes
0
answers
86
views
Issue regarding flames game in shell script
I am trying to make a flames game in shell script. I take 2 names and sort names. My code like this: ```lang-bash var=$(j=1;for((i=0;i ]/{printf $2}') #checking for differernt characters using diff and folding using fold to newline after every 1 character (fold -w1) ``` Now the actual problem I am f...
I am trying to make a flames game in shell script. I take 2 names and sort names.
My code like this:
-bash
var=$(j=1;for((i=0;i]/{printf $2}') #checking for differernt characters using diff and folding using fold to newline after every 1 character (fold -w1)
Now the actual problem I am facing is in the flames
part. I am running a loop to check for the length of the different sting and at that position or iteration whatever character is there to remove it from this:
-bash
flames()
{
echo "$word ${#word}"
size=echo ${#var2}
for((i=0;i<${#word};i++))
do
echo " ${word:$i:1}"
if [[ $i == $size ]]
then
cha=echo ${word:$i:1}
word1=echo ${word//$cha/}
fi
done
word=echo $word1
echo "$word ${#word}"
}
What its doing is its working till the value of size is equal to the i
value . After that I am unable to understand how to check for and remove till there is just one character left?
-bash
while [ ${#word} -ne 1 ]
do
flames
done
The above portion is running infinitely . Also is there any other way to doing it better than actually doing the parameter expansion?
utkarsh dubey
(11 rep)
Aug 21, 2023, 04:37 PM
• Last activity: Aug 23, 2023, 09:28 AM
0
votes
1
answers
154
views
Bash variable quoting in a function
I've defined a short helper function in a bash script. For legibility, I'd like to replace the boilerplate headers with a variable declared outside the function. I've tried quoting the declaration many different ways, but nothing's working. I want to replace this put() { local response=$(curl -H 'Co...
I've defined a short helper function in a bash script. For legibility, I'd like to replace the boilerplate headers with a variable declared outside the function.
I've tried quoting the declaration many different ways, but nothing's working.
I want to replace this
put() {
local response=$(curl -H 'Content-Type: application/json' -H 'Accept: application/vnd.urbanairship+json; version=3;' ...)
echo $response
}
with something like this
headers=???
put() {
local response=$(curl $headers ...)
echo $response
}
i.e.
$headers
should expand to -H 'Content-Type: application/json' -H 'Accept: application/vnd.urbanairship+json; version=3;'
$headers
needs to expand to separate words to get passed properly as arguments, so I've even tried using an array.
Note: I've pretty much given up. It's not worth fighting with for such a simple use. At this point the question is for edification.
Hod
(101 rep)
Feb 14, 2017, 01:56 AM
• Last activity: May 31, 2023, 05:33 PM
13
votes
3
answers
27786
views
How to pass all arguments of a function along to another command?
Hello I have this in my `~/.bash_profile` export GOPATH="$HOME/go_projects" export GOBIN="$GOPATH/bin" program(){ $GOBIN/program $1 } so I'm able to do `program "-p hello_world -tSu"`. Is there any way to run the program and custom flags without using the quotation marks? if I do just `program -p he...
Hello I have this in my
~/.bash_profile
export GOPATH="$HOME/go_projects"
export GOBIN="$GOPATH/bin"
program(){
$GOBIN/program $1
}
so I'm able to do program "-p hello_world -tSu"
. Is there any way to run the program and custom flags without using the quotation marks? if I do just program -p hello_world -tSu
it'll only use the -p
flag and everything after the space will be ignored.
nooby
(141 rep)
Nov 23, 2020, 12:21 PM
• Last activity: May 2, 2023, 10:46 PM
17
votes
2
answers
6900
views
Which are the (Bash) shell special parameters/variables?
I've found some special parameters in Bash that start with `$` (the dollar sign). For example, when I wanted to know the exit status, I used `$?`. For getting the process ID, there's [`$$`](https://askubuntu.com/q/496054). What are the special Bash (shell) parameters and their usage?
I've found some special parameters in Bash that start with
$
(the dollar sign).
For example, when I wanted to know the exit status, I used $?
. For getting the process ID, there's [$$
](https://askubuntu.com/q/496054) .
What are the special Bash (shell) parameters and their usage?
Pandya
(25613 rep)
Jul 25, 2015, 06:22 AM
• Last activity: Feb 19, 2023, 03:09 PM
7
votes
3
answers
959
views
Most robust way to list every basename in a directory, sorted by modification date?
Given a directory containing: * `note 1.txt`, last modified yesterday * `note 2.txt`, last modified the day before yesterday * `note 3.txt`, last modified today What is the best way to fetch the array `note 3` `note 1` `note 2`? To define "best," I'm more concerned about robustness (in the context o...
Given a directory containing:
*
note 1.txt
, last modified yesterday
* note 2.txt
, last modified the day before yesterday
* note 3.txt
, last modified today
What is the best way to fetch the array note 3
note 1
note 2
?
To define "best," I'm more concerned about robustness (in the context of Zsh in macOS) than I am about efficiency and portability.
The intended use case is a directory of hundreds or thousands of plain text files, but—at the risk of muddling the question—this is a specific case of a more general question I have, of what best practices are in performing string manipulations on filepaths printed by commands like ls
, find
, and mdfind
.
---
I've been using a macro which invokes this command to achieve the above:
ls -t | sed -e 's/.[^.]*$//'
It's never failed, but:
* [Greg's Wiki](http://mywiki.wooledge.org/EnglishFrontPage) strongly recommends against parsing the output of ls
. ([Parsing ls
](http://mywiki.wooledge.org/ParsingLs) ; [Practices](http://mywiki.wooledge.org/BashGuide/Practices) , under "5. Don't Ever Do These").
* Is invoking sed
inefficient where parameter expansion would do?
Using find
(safely delimiting filepaths with NUL
characters rather than newlines), and parameter expansion to extract the basenames, this produces an unsorted list:
find . -type f -print0 | while IFS= read -d '' -r l ; do print "${${l%.*}##*/}" ; done
But sorting by modification date would seem to require invoking stat
and sort
, because macOS's find
lacks the -printf
flag which [might otherwise serve well](https://superuser.com/a/546900) .
Finally, using Zsh's [glob qualifiers](http://zsh.sourceforge.net/Doc/Release/Expansion.html#Glob-Qualifiers) :
for f in *(om) ; do print "${f%.*}" ; done
Though not portable, this last method seems most robust and efficient to me. Is this correct, and is there any reason I shouldn't use a modified version of the find
command above when I'm actually performing a search rather than simply listing files in a directory?
seanakabry
(73 rep)
Jul 25, 2020, 08:23 AM
• Last activity: Jan 19, 2023, 04:17 AM
Showing page 1 of 20 total questions