QuestionId stringlengths 3 8 | QuestionTitle stringlengths 15 149 | QuestionScore int64 1 27.2k | QuestionCreatedUtc stringdate 2008-08-01 18:33:08 2025-06-16 04:59:48 | QuestionBodyHtml stringlengths 40 28.2k | QuestionViewCount int64 78 16.5M | QuestionOwnerUserId float64 4 25.2M ⌀ | QuestionLastActivityUtc stringdate 2008-09-08 19:26:01 2026-01-24 22:40:25 | AcceptedAnswerId int64 266 79.7M | AnswerScore int64 2 30.1k | AnswerCreatedUtc stringdate 2008-08-01 23:40:28 2025-06-16 05:23:39 | AnswerOwnerUserId float64 1 29.5M ⌀ | AnswerLastActivityUtc stringdate 2008-08-01 23:40:28 2026-01-23 17:46:09 | QuestionLink stringlengths 31 36 | AnswerLink stringlengths 31 36 | AnswerBodyHtml stringlengths 35 33k | AnswerBodyPreview stringlengths 35 400 | AllTagIdsCsv stringlengths 2 37 | AllTagNamesCsv stringlengths 2 106 | MergedQuestion stringlengths 74 28.4k | quality_filter stringclasses 10
values | unique_id int64 0 29k | tag_major_lang stringclasses 10
values | MergedQuestion_md stringlengths 60 28.1k | AnswerBodyHtml_md stringlengths 14 29.2k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3004811 | How do you run multiple programs in parallel from a bash script? | 568 | 2010-06-09 10:06:18 | <p>I am trying to write a <strong>.sh file</strong> that runs many programs <strong>simultaneously</strong></p>
<p>I tried this</p>
<pre><code>prog1
prog2
</code></pre>
<p>But that runs prog1 then waits until prog1 ends and then starts prog2...</p>
<p>So how can I run them in parallel?</p>
| 731,267 | 279,691 | 2025-09-09 16:28:06 | 3,004,814 | 569 | 2010-06-09 10:07:14 | 333,698 | 2024-08-31 16:32:59 | https://stackoverflow.com/q/3004811 | https://stackoverflow.com/a/3004814 | <p>To run multiple programs in parallel:</p>
<pre><code>prog1 &
prog2 &
</code></pre>
<p>If you need your script to wait for the programs to finish, you can add:</p>
<pre><code>wait
</code></pre>
<p>at the point where you want the script to wait for them. For example:</p>
<pre><code>prog1 &
prog2 &
# (d... | <p>To run multiple programs in parallel:</p> <pre><code>prog1 & prog2 & </code></pre> <p>If you need your script to wait for the programs to finish, you can add:</p> <pre><code>wait </code></pre> <p>at the point where you want the script to wait for them. For example:</p> <pre><code>prog1 & prog2 & # (d... | 353, 387 | bash, parallel-processing | <h1>How do you run multiple programs in parallel from a bash script?</h1>
<p>I am trying to write a <strong>.sh file</strong> that runs many programs <strong>simultaneously</strong></p>
<p>I tried this</p>
<pre><code>prog1
prog2
</code></pre>
<p>But that runs prog1 then waits until prog1 ends and then starts prog2.... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 316 | bash | # How do you run multiple programs in parallel from a bash script?
I am trying to write a **.sh file** that runs many programs **simultaneously**
I tried this
```
prog1
prog2
```
But that runs prog1 then waits until prog1 ends and then starts prog2...
So how can I run them in parallel? | To run multiple programs in parallel:
```
prog1 &
prog2 &
```
If you need your script to wait for the programs to finish, you can add:
```
wait
```
at the point where you want the script to wait for them. For example:
```
prog1 &
prog2 &
# (do other things while 'prog1' and 'prog2' carry on
# in the background)
..... |
16853649 | How to execute a Python script from the Django shell? | 371 | 2013-05-31 09:05:36 | <p>I need to execute a Python script from the Django shell. I tried:</p>
<pre><code>./manage.py shell << my_script.py
</code></pre>
<p>But it didn't work. It was just waiting for me to write something.</p>
| 370,693 | null | 2023-06-01 20:17:35 | 16,853,799 | 569 | 2013-05-31 09:13:21 | 3,571 | 2017-06-22 08:00:42 | https://stackoverflow.com/q/16853649 | https://stackoverflow.com/a/16853799 | <p>The <code><<</code> part is wrong, use <code><</code> instead:</p>
<pre><code>$ ./manage.py shell < myscript.py
</code></pre>
<p>You could also do:</p>
<pre><code>$ ./manage.py shell
...
>>> execfile('myscript.py')
</code></pre>
<p>For python3 you would need to use</p>
<pre><code>>>&g... | <p>The <code><<</code> part is wrong, use <code><</code> instead:</p> <pre><code>$ ./manage.py shell < myscript.py </code></pre> <p>You could also do:</p> <pre><code>$ ./manage.py shell ... >>> execfile('myscript.py') </code></pre> <p>For python3 you would need to use</p> <pre><code>>>&g... | 16, 243, 55804 | django, django-shell, python | <h1>How to execute a Python script from the Django shell?</h1>
<p>I need to execute a Python script from the Django shell. I tried:</p>
<pre><code>./manage.py shell << my_script.py
</code></pre>
<p>But it didn't work. It was just waiting for me to write something.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 317 | bash | # How to execute a Python script from the Django shell?
I need to execute a Python script from the Django shell. I tried:
```
./manage.py shell << my_script.py
```
But it didn't work. It was just waiting for me to write something. | The `<<` part is wrong, use `<` instead:
```
$ ./manage.py shell < myscript.py
```
You could also do:
```
$ ./manage.py shell
...
>>> execfile('myscript.py')
```
For python3 you would need to use
```
>>> exec(open('myscript.py').read())
``` |
22620393 | Various ways to remove local Git changes | 714 | 2014-03-24 20:54:39 | <p>I just cloned a git repository and checked out a branch. I worked on it, and then decided to remove all my local changes, as I wanted the original copy.</p>
<p>In short, I had to do the following two commands to remove my local changes</p>
<pre><code>git checkout .
git clean -f
</code></pre>
<p><strong>My questi... | 782,488 | 3,250,087 | 2020-12-21 18:59:55 | 22,621,464 | 567 | 2014-03-24 21:57:47 | 430,885 | 2016-02-04 16:49:56 | https://stackoverflow.com/q/22620393 | https://stackoverflow.com/a/22621464 | <p>It all depends on exactly what you are trying to undo/revert. Start out by reading <a href="https://stackoverflow.com/questions/1146973/how-do-i-revert-all-local-changes-in-a-git-managed-project-to-previous-state">the post in Ube's link</a>. But to attempt an answer:</p>
<p><strong>Hard reset</strong></p>
<pre><co... | <p>It all depends on exactly what you are trying to undo/revert. Start out by reading <a href="https://stackoverflow.com/questions/1146973/how-do-i-revert-all-local-changes-in-a-git-managed-project-to-previous-state">the post in Ube's link</a>. But to attempt an answer:</p> <p><strong>Hard reset</strong></p> <pre><co... | 119, 61874 | git, git-bash | <h1>Various ways to remove local Git changes</h1>
<p>I just cloned a git repository and checked out a branch. I worked on it, and then decided to remove all my local changes, as I wanted the original copy.</p>
<p>In short, I had to do the following two commands to remove my local changes</p>
<pre><code>git checkout .... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 318 | bash | # Various ways to remove local Git changes
I just cloned a git repository and checked out a branch. I worked on it, and then decided to remove all my local changes, as I wanted the original copy.
In short, I had to do the following two commands to remove my local changes
```
git checkout .
git clean -f
```
**My qu... | It all depends on exactly what you are trying to undo/revert. Start out by reading [the post in Ube's link](https://stackoverflow.com/questions/1146973/how-do-i-revert-all-local-changes-in-a-git-managed-project-to-previous-state). But to attempt an answer:
**Hard reset**
```
git reset --hard [HEAD]
```
*completely r... |
8426058 | Getting the parent of a directory in Bash | 366 | 2011-12-08 04:12:08 | <p>If I have a file path such as...</p>
<pre><code>/home/smith/Desktop/Test
/home/smith/Desktop/Test/
</code></pre>
<p>How do I change the string so it will be the parent directory?</p>
<p>e.g.</p>
<pre><code>/home/smith/Desktop
/home/smith/Desktop/
</code></pre>
| 403,658 | 593,884 | 2024-01-02 16:39:45 | 8,426,110 | 567 | 2011-12-08 04:19:54 | 494,061 | 2024-01-02 16:39:45 | https://stackoverflow.com/q/8426058 | https://stackoverflow.com/a/8426110 | <pre><code>dir=/home/smith/Desktop/Test
parentdir="$(dirname "$dir")"
</code></pre>
<p>Works if there is a trailing slash, too.</p>
| <pre><code>dir=/home/smith/Desktop/Test parentdir="$(dirname "$dir")" </code></pre> <p>Works if there is a trailing slash, too.</p> | 218, 387, 37484 | bash, directory, dirname | <h1>Getting the parent of a directory in Bash</h1>
<p>If I have a file path such as...</p>
<pre><code>/home/smith/Desktop/Test
/home/smith/Desktop/Test/
</code></pre>
<p>How do I change the string so it will be the parent directory?</p>
<p>e.g.</p>
<pre><code>/home/smith/Desktop
/home/smith/Desktop/
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 319 | bash | # Getting the parent of a directory in Bash
If I have a file path such as...
```
/home/smith/Desktop/Test
/home/smith/Desktop/Test/
```
How do I change the string so it will be the parent directory?
e.g.
```
/home/smith/Desktop
/home/smith/Desktop/
``` | ```
dir=/home/smith/Desktop/Test
parentdir="$(dirname "$dir")"
```
Works if there is a trailing slash, too. |
3513650 | Timing a command's execution in PowerShell | 355 | 2010-08-18 15:17:11 | <p>Is there a simple way to time the execution of a command in PowerShell, like the 'time' command in Linux?<br>
I came up with this:</p>
<pre><code>$s=Get-Date; .\do_something.ps1 ; $e=Get-Date; ($e - $s).TotalSeconds
</code></pre>
<p>But I would like something simpler like</p>
<pre><code>time .\do_something.ps1
</... | 249,042 | 15,622 | 2025-12-05 15:09:14 | 3,513,669 | 566 | 2010-08-18 15:18:57 | 153,982 | 2020-04-20 22:30:51 | https://stackoverflow.com/q/3513650 | https://stackoverflow.com/a/3513669 | <p>Yup.</p>
<pre><code>Measure-Command { .\do_something.ps1 }
</code></pre>
<p>Note that one minor downside of <code>Measure-Command</code> is that you see no <code>stdout</code> output. </p>
<p>[Update, thanks to @JasonMArcher] You can fix that by piping the command output to some commandlet that writes to the hos... | <p>Yup.</p> <pre><code>Measure-Command { .\do_something.ps1 } </code></pre> <p>Note that one minor downside of <code>Measure-Command</code> is that you see no <code>stdout</code> output. </p> <p>[Update, thanks to @JasonMArcher] You can fix that by piping the command output to some commandlet that writes to the hos... | 526, 603, 15737 | performance-testing, powershell, time | <h1>Timing a command's execution in PowerShell</h1>
<p>Is there a simple way to time the execution of a command in PowerShell, like the 'time' command in Linux?<br>
I came up with this:</p>
<pre><code>$s=Get-Date; .\do_something.ps1 ; $e=Get-Date; ($e - $s).TotalSeconds
</code></pre>
<p>But I would like something sim... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 320 | bash | # Timing a command's execution in PowerShell
Is there a simple way to time the execution of a command in PowerShell, like the 'time' command in Linux?
I came up with this:
```
$s=Get-Date; .\do_something.ps1 ; $e=Get-Date; ($e - $s).TotalSeconds
```
But I would like something simpler like
```
time .\do_something.... | Yup.
```
Measure-Command { .\do_something.ps1 }
```
Note that one minor downside of `Measure-Command` is that you see no `stdout` output.
[Update, thanks to @JasonMArcher] You can fix that by piping the command output to some commandlet that writes to the host, e.g. `Out-Default` so it becomes:
```
Measure-Command ... |
537942 | How to list running screen sessions? | 329 | 2009-02-11 17:47:34 | <p>I have a bunch of servers, on which I run experiments using <code>screen</code>. The procedure is the following :</p>
<ol>
<li><code>ssh</code> to server XXX</li>
<li>launch <code>screen</code></li>
<li>start experiments in a few tabs</li>
<li>detach <code>screen</code></li>
<li>disconnect from the server</li>
</ol... | 775,223 | 56,761 | 2025-01-21 01:08:35 | 537,980 | 564 | 2009-02-11 17:56:04 | 30,587 | 2015-11-11 06:58:43 | https://stackoverflow.com/q/537942 | https://stackoverflow.com/a/537980 | <p>To list all of the screen sessions for a user, run the following command as that user:</p>
<pre><code>screen -ls
</code></pre>
<p>To see all screen sessions on a specific machine you can do:</p>
<pre><code>ls -laR /var/run/screen/
</code></pre>
<p>I get this on my machine:</p>
<pre><code>gentle ~ # ls -laR /var... | <p>To list all of the screen sessions for a user, run the following command as that user:</p> <pre><code>screen -ls </code></pre> <p>To see all screen sessions on a specific machine you can do:</p> <pre><code>ls -laR /var/run/screen/ </code></pre> <p>I get this on my machine:</p> <pre><code>gentle ~ # ls -laR /var... | 58, 387, 1231, 5024 | bash, command-line, gnu-screen, linux | <h1>How to list running screen sessions?</h1>
<p>I have a bunch of servers, on which I run experiments using <code>screen</code>. The procedure is the following :</p>
<ol>
<li><code>ssh</code> to server XXX</li>
<li>launch <code>screen</code></li>
<li>start experiments in a few tabs</li>
<li>detach <code>screen</code>... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 321 | bash | # How to list running screen sessions?
I have a bunch of servers, on which I run experiments using `screen`. The procedure is the following :
1. `ssh` to server XXX
2. launch `screen`
3. start experiments in a few tabs
4. detach `screen`
5. disconnect from the server
While the experiments are running, I can easily f... | To list all of the screen sessions for a user, run the following command as that user:
```
screen -ls
```
To see all screen sessions on a specific machine you can do:
```
ls -laR /var/run/screen/
```
I get this on my machine:
```
gentle ~ # ls -laR /var/run/screen/
/var/run/screen/:
total 1
drwxrwxr-x 4 root utm... |
2556190 | Random number from a range in a Bash Script | 300 | 2010-03-31 20:21:04 | <p>I need to generate a random port number between <code>2000-65000</code> from a shell script. The problem is <code>$RANDOM</code> is a 15-bit number, so I'm stuck!</p>
<p><code>PORT=$(($RANDOM%63000+2001))</code> would work nicely if it wasn't for the size limitation.</p>
<p>Does anyone have an example of how I can... | 277,095 | 276,486 | 2024-01-28 19:21:31 | 2,556,282 | 561 | 2010-03-31 20:33:43 | 115,478 | 2010-03-31 20:33:43 | https://stackoverflow.com/q/2556190 | https://stackoverflow.com/a/2556282 | <pre><code>shuf -i 2000-65000 -n 1
</code></pre>
<p>Enjoy!</p>
<p><em>Edit</em>: The range is inclusive.</p>
| <pre><code>shuf -i 2000-65000 -n 1 </code></pre> <p>Enjoy!</p> <p><em>Edit</em>: The range is inclusive.</p> | 387, 390, 531 | bash, scripting, shell | <h1>Random number from a range in a Bash Script</h1>
<p>I need to generate a random port number between <code>2000-65000</code> from a shell script. The problem is <code>$RANDOM</code> is a 15-bit number, so I'm stuck!</p>
<p><code>PORT=$(($RANDOM%63000+2001))</code> would work nicely if it wasn't for the size limitat... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 322 | bash | # Random number from a range in a Bash Script
I need to generate a random port number between `2000-65000` from a shell script. The problem is `$RANDOM` is a 15-bit number, so I'm stuck!
`PORT=$(($RANDOM%63000+2001))` would work nicely if it wasn't for the size limitation.
Does anyone have an example of how I can do... | ```
shuf -i 2000-65000 -n 1
```
Enjoy!
*Edit*: The range is inclusive. |
10508843 | What is /dev/null 2>&1? | 434 | 2012-05-09 01:46:19 | <p>I found this piece of code in /etc/cron.daily/apf </p>
<pre><code>#!/bin/bash
/etc/apf/apf -f >> /dev/null 2>&1
/etc/apf/apf -s >> /dev/null 2>&1
</code></pre>
<p>It's flushing and reloading the firewall.<br>
I don't understand the <code>>> /dev/null 2>&1</code> part... | 575,600 | 440,646 | 2022-10-19 14:43:29 | 10,508,862 | 557 | 2012-05-09 01:49:42 | 722,332 | 2014-04-09 12:27:20 | https://stackoverflow.com/q/10508843 | https://stackoverflow.com/a/10508862 | <p><code>>> /dev/null</code> redirects standard output (<code>stdout</code>) to <code>/dev/null</code>, which discards it.</p>
<p>(The <code>>></code> seems sort of superfluous, since <code>>></code> means append while <code>></code> means truncate and write, and either appending to or writing to ... | <p><code>>> /dev/null</code> redirects standard output (<code>stdout</code>) to <code>/dev/null</code>, which discards it.</p> <p>(The <code>>></code> seems sort of superfluous, since <code>>></code> means append while <code>></code> means truncate and write, and either appending to or writing to ... | 367, 390, 1993, 26698 | io-redirection, posix, shell, syntax | <h1>What is /dev/null 2>&1?</h1>
<p>I found this piece of code in /etc/cron.daily/apf </p>
<pre><code>#!/bin/bash
/etc/apf/apf -f >> /dev/null 2>&1
/etc/apf/apf -s >> /dev/null 2>&1
</code></pre>
<p>It's flushing and reloading the firewall.<br>
I don't understand the <code>>> ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 323 | bash | # What is /dev/null 2>&1?
I found this piece of code in /etc/cron.daily/apf
```
#!/bin/bash
/etc/apf/apf -f >> /dev/null 2>&1
/etc/apf/apf -s >> /dev/null 2>&1
```
It's flushing and reloading the firewall.
I don't understand the `>> /dev/null 2>&1` part.
What is the purpose of having this in the cron? It's ov... | `>> /dev/null` redirects standard output (`stdout`) to `/dev/null`, which discards it.
(The `>>` seems sort of superfluous, since `>>` means append while `>` means truncate and write, and either appending to or writing to `/dev/null` has the same net effect. I usually just use `>` for that reason.)
`2>&1` redirects s... |
3061036 | How to find whether or not a variable is empty in Bash | 384 | 2010-06-17 11:00:37 | <p>How can I check if a variable is empty in Bash?</p>
| 534,438 | 352,860 | 2023-11-23 08:57:26 | 3,063,887 | 557 | 2010-06-17 17:09:32 | 79,737 | 2022-10-01 14:15:33 | https://stackoverflow.com/q/3061036 | https://stackoverflow.com/a/3063887 | <p>In Bash at least the following command tests <strong>if $var is empty</strong>:</p>
<pre><code>if [[ -z "$var" ]]; then
# $var is empty, do what you want
fi
</code></pre>
<p>The command <code>man test</code> is your friend.</p>
| <p>In Bash at least the following command tests <strong>if $var is empty</strong>:</p> <pre><code>if [[ -z "$var" ]]; then # $var is empty, do what you want fi </code></pre> <p>The command <code>man test</code> is your friend.</p> | 387, 390, 58757 | bash, is-empty, shell | <h1>How to find whether or not a variable is empty in Bash</h1>
<p>How can I check if a variable is empty in Bash?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 324 | bash | # How to find whether or not a variable is empty in Bash
How can I check if a variable is empty in Bash? | In Bash at least the following command tests **if $var is empty**:
```
if [[ -z "$var" ]]; then
# $var is empty, do what you want
fi
```
The command `man test` is your friend. |
6980090 | How to read from a file or standard input in Bash | 341 | 2011-08-08 09:27:56 | <p>The following Perl script (<code>my.pl</code>) can read from either the file in the command line arguments or from <a href="https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)" rel="noreferrer">standard input</a> (STDIN):</p>
<pre><code>while (<>) {
print($_);
}
</code></pre>
<p><code>perl... | 576,872 | 404,264 | 2023-09-13 14:41:54 | 7,045,517 | 557 | 2011-08-12 19:44:50 | 57,457 | 2021-06-21 12:48:12 | https://stackoverflow.com/q/6980090 | https://stackoverflow.com/a/7045517 | <p>The following solution reads from a file if the script is called with a file name as the first parameter <code>$1</code> and otherwise from standard input.</p>
<pre><code>while read line
do
echo "$line"
done < "${1:-/dev/stdin}"
</code></pre>
<p>The substitution <code>${1:-...}</code> takes ... | <p>The following solution reads from a file if the script is called with a file name as the first parameter <code>$1</code> and otherwise from standard input.</p> <pre><code>while read line do echo "$line" done < "${1:-/dev/stdin}" </code></pre> <p>The substitution <code>${1:-...}</code> takes ... | 387, 1701 | bash, stdin | <h1>How to read from a file or standard input in Bash</h1>
<p>The following Perl script (<code>my.pl</code>) can read from either the file in the command line arguments or from <a href="https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)" rel="noreferrer">standard input</a> (STDIN):</p>
<pre><code>whi... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 325 | bash | # How to read from a file or standard input in Bash
The following Perl script (`my.pl`) can read from either the file in the command line arguments or from [standard input](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)) (STDIN):
```
while (<>) {
print($_);
}
```
`perl my.pl` will read from... | The following solution reads from a file if the script is called with a file name as the first parameter `$1` and otherwise from standard input.
```
while read line
do
echo "$line"
done < "${1:-/dev/stdin}"
```
The substitution `${1:-...}` takes `$1` if defined. Otherwise, the file name of the standard input of the... |
5928156 | Replace one character with another in Bash | 326 | 2011-05-08 14:55:37 | <p>I need to replace a space (<code> </code>) with a dot (<code>.</code>) in a string in bash.</p>
<p>I think this would be pretty simple, but I'm new so I can't figure out how to modify a similar example for this use.</p>
| 395,633 | 728,236 | 2022-12-30 01:12:27 | 5,928,254 | 557 | 2011-05-08 15:11:31 | 53,495 | 2011-05-08 15:11:31 | https://stackoverflow.com/q/5928156 | https://stackoverflow.com/a/5928254 | <p>Use inline shell string replacement. Example:</p>
<pre><code>foo=" "
# replace first blank only
bar=${foo/ /.}
# replace all blanks
bar=${foo// /.}
</code></pre>
<p>See <a href="http://tldp.org/LDP/abs/html/string-manipulation.html">http://tldp.org/LDP/abs/html/string-manipulation.html</a> for more details.</p>... | <p>Use inline shell string replacement. Example:</p> <pre><code>foo=" " # replace first blank only bar=${foo/ /.} # replace all blanks bar=${foo// /.} </code></pre> <p>See <a href="http://tldp.org/LDP/abs/html/string-manipulation.html">http://tldp.org/LDP/abs/html/string-manipulation.html</a> for more details.</p>... | 139, 387, 2498 | bash, replace, string | <h1>Replace one character with another in Bash</h1>
<p>I need to replace a space (<code> </code>) with a dot (<code>.</code>) in a string in bash.</p>
<p>I think this would be pretty simple, but I'm new so I can't figure out how to modify a similar example for this use.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 326 | bash | # Replace one character with another in Bash
I need to replace a space () with a dot (`.`) in a string in bash.
I think this would be pretty simple, but I'm new so I can't figure out how to modify a similar example for this use. | Use inline shell string replacement. Example:
```
foo=" "
# replace first blank only
bar=${foo/ /.}
# replace all blanks
bar=${foo// /.}
```
See <http://tldp.org/LDP/abs/html/string-manipulation.html> for more details. |
742466 | How can I reverse the order of lines in a file? | 900 | 2009-04-12 21:34:53 | <p>I'd like to reverse the order of lines in a text file (or stdin), preserving the contents of each line.</p>
<p>So, i.e., starting with:</p>
<pre><code>foo
bar
baz
</code></pre>
<p>I'd like to end up with </p>
<pre><code>baz
bar
foo
</code></pre>
<p>Is there a standard UNIX commandline utility for this?</p>
| 506,246 | 41,177 | 2025-06-24 13:27:17 | 742,469 | 554 | 2009-04-12 21:36:30 | 4,926 | 2014-12-09 17:50:02 | https://stackoverflow.com/q/742466 | https://stackoverflow.com/a/742469 | <p>BSD tail:</p>
<pre><code>tail -r myfile.txt
</code></pre>
<p>Reference: <a href="https://www.freebsd.org/cgi/man.cgi?query=tail&apropos=0&sektion=1" rel="noreferrer">FreeBSD</a>, <a href="http://netbsd.gw.com/cgi-bin/man-cgi?tail+1" rel="noreferrer">NetBSD</a>, <a href="http://www.openbsd.org/cgi-bin/man.c... | <p>BSD tail:</p> <pre><code>tail -r myfile.txt </code></pre> <p>Reference: <a href="https://www.freebsd.org/cgi/man.cgi?query=tail&apropos=0&sektion=1" rel="noreferrer">FreeBSD</a>, <a href="http://netbsd.gw.com/cgi-bin/man-cgi?tail+1" rel="noreferrer">NetBSD</a>, <a href="http://www.openbsd.org/cgi-bin/man.c... | 34, 390, 1231 | command-line, shell, unix | <h1>How can I reverse the order of lines in a file?</h1>
<p>I'd like to reverse the order of lines in a text file (or stdin), preserving the contents of each line.</p>
<p>So, i.e., starting with:</p>
<pre><code>foo
bar
baz
</code></pre>
<p>I'd like to end up with </p>
<pre><code>baz
bar
foo
</code></pre>
<p>Is the... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 327 | bash | # How can I reverse the order of lines in a file?
I'd like to reverse the order of lines in a text file (or stdin), preserving the contents of each line.
So, i.e., starting with:
```
foo
bar
baz
```
I'd like to end up with
```
baz
bar
foo
```
Is there a standard UNIX commandline utility for this? | BSD tail:
```
tail -r myfile.txt
```
Reference: [FreeBSD](https://www.freebsd.org/cgi/man.cgi?query=tail&apropos=0&sektion=1), [NetBSD](http://netbsd.gw.com/cgi-bin/man-cgi?tail+1), [OpenBSD](http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man1/tail.1?query=tail&sec=1) and [OS X](https://developer.apple.com/li... |
17583578 | What command means "do nothing" in a conditional in Bash? | 297 | 2013-07-11 01:24:37 | <p>Sometimes when making conditionals, I need the code to do nothing, e.g., here, I want Bash to do nothing when <code>$a</code> is greater than "10", print "1" if <code>$a</code> is less than "5", otherwise, print "2":</p>
<pre><code>if [ "$a" -ge 10 ]
then
elif [ "$a" -le 5 ]
then
echo "1"
else
echo "2"
fi
<... | 259,425 | 834,616 | 2024-03-12 18:20:05 | 17,583,599 | 554 | 2013-07-11 01:26:38 | 1,491,895 | 2017-05-26 19:31:34 | https://stackoverflow.com/q/17583578 | https://stackoverflow.com/a/17583599 | <p>The no-op command in shell is <code>:</code> (colon).</p>
<pre><code>if [ "$a" -ge 10 ]
then
:
elif [ "$a" -le 5 ]
then
echo "1"
else
echo "2"
fi
</code></pre>
<p>From the <a href="https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#Bourne-Shell-Builtins" rel="noreferrer">bash... | <p>The no-op command in shell is <code>:</code> (colon).</p> <pre><code>if [ "$a" -ge 10 ] then : elif [ "$a" -le 5 ] then echo "1" else echo "2" fi </code></pre> <p>From the <a href="https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#Bourne-Shell-Builtins" rel="noreferrer">bash... | 387, 39271, 39458 | bash, conditional-statements, no-op | <h1>What command means "do nothing" in a conditional in Bash?</h1>
<p>Sometimes when making conditionals, I need the code to do nothing, e.g., here, I want Bash to do nothing when <code>$a</code> is greater than "10", print "1" if <code>$a</code> is less than "5", otherwise, print "2":</p>
<pre><code>if [ "$a" -ge 10 ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 328 | bash | # What command means "do nothing" in a conditional in Bash?
Sometimes when making conditionals, I need the code to do nothing, e.g., here, I want Bash to do nothing when `$a` is greater than "10", print "1" if `$a` is less than "5", otherwise, print "2":
```
if [ "$a" -ge 10 ]
then
elif [ "$a" -le 5 ]
then
echo "... | The no-op command in shell is `:` (colon).
```
if [ "$a" -ge 10 ]
then
:
elif [ "$a" -le 5 ]
then
echo "1"
else
echo "2"
fi
```
From the [bash manual](https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#Bourne-Shell-Builtins):
> `:` (a colon)
> Do nothing beyond expanding argu... |
12314451 | Accessing bash command line args $@ vs $* | 449 | 2012-09-07 08:28:46 | <p>In many SO questions and bash tutorials I see that I can access command line args in bash scripts in two ways:</p>
<pre><code>$ ~ >cat testargs.sh
#!/bin/bash
echo "you passed me" $*
echo "you passed me" $@
</code></pre>
<p>Which results in:</p>
<pre><code>$ ~> bash testargs.sh arg1 arg2
you passed me arg... | 269,997 | 492,620 | 2024-07-05 14:01:32 | 12,316,565 | 553 | 2012-09-07 10:46:32 | 7,552 | 2014-02-22 11:20:00 | https://stackoverflow.com/q/12314451 | https://stackoverflow.com/a/12316565 | <p>The difference appears when the special parameters are quoted. Let me illustrate the differences:</p>
<pre><code>$ set -- "arg 1" "arg 2" "arg 3"
$ for word in $*; do echo "$word"; done
arg
1
arg
2
arg
3
$ for word in $@; do echo "$word"; done
arg
1
arg
2
arg
3
$ for word in "$*"; do echo "$word"; done
arg 1... | <p>The difference appears when the special parameters are quoted. Let me illustrate the differences:</p> <pre><code>$ set -- "arg 1" "arg 2" "arg 3" $ for word in $*; do echo "$word"; done arg 1 arg 2 arg 3 $ for word in $@; do echo "$word"; done arg 1 arg 2 arg 3 $ for word in "$*"; do echo "$word"; done arg 1... | 387, 31134 | bash, command-line-arguments | <h1>Accessing bash command line args $@ vs $*</h1>
<p>In many SO questions and bash tutorials I see that I can access command line args in bash scripts in two ways:</p>
<pre><code>$ ~ >cat testargs.sh
#!/bin/bash
echo "you passed me" $*
echo "you passed me" $@
</code></pre>
<p>Which results in:</p>
<pre><code>$... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 329 | bash | # Accessing bash command line args $@ vs $*
In many SO questions and bash tutorials I see that I can access command line args in bash scripts in two ways:
```
$ ~ >cat testargs.sh
#!/bin/bash
echo "you passed me" $*
echo "you passed me" $@
```
Which results in:
```
$ ~> bash testargs.sh arg1 arg2
you passed me ar... | The difference appears when the special parameters are quoted. Let me illustrate the differences:
```
$ set -- "arg 1" "arg 2" "arg 3"
$ for word in $*; do echo "$word"; done
arg
1
arg
2
arg
3
$ for word in $@; do echo "$word"; done
arg
1
arg
2
arg
3
$ for word in "$*"; do echo "$word"; done
arg 1 arg 2 arg 3... |
2608144 | How to split long commands over multiple lines in PowerShell | 360 | 2010-04-09 14:11:01 | <p>How do you take a command like the following in PowerShell and split it across multiple lines?</p>
<pre><code>&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" -verb:sync -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" -dest:contentPath="c:\websites\xxx\wwwroot\,computerNa... | 350,940 | 64,105 | 2020-02-27 22:06:25 | 2,608,186 | 550 | 2010-04-09 14:17:21 | 12,744 | 2020-01-27 14:58:33 | https://stackoverflow.com/q/2608144 | https://stackoverflow.com/a/2608186 | <p>Trailing backtick character, i.e.,</p>
<pre><code>&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" `
-verb:sync `
-source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" `
-dest:contentPath="c:\websites\xxx\wwwroot,computerName=192.168.1.1,username=administrator,password=xxx"
</c... | <p>Trailing backtick character, i.e.,</p> <pre><code>&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" ` -verb:sync ` -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" ` -dest:contentPath="c:\websites\xxx\wwwroot,computerName=192.168.1.1,username=administrator,password=xxx" </c... | 526 | powershell | <h1>How to split long commands over multiple lines in PowerShell</h1>
<p>How do you take a command like the following in PowerShell and split it across multiple lines?</p>
<pre><code>&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" -verb:sync -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWe... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 330 | bash | # How to split long commands over multiple lines in PowerShell
How do you take a command like the following in PowerShell and split it across multiple lines?
```
&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" -verb:sync -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" -dest:co... | Trailing backtick character, i.e.,
```
&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" `
-verb:sync `
-source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" `
-dest:contentPath="c:\websites\xxx\wwwroot,computerName=192.168.1.1,username=administrator,password=xxx"
```
White space matt... |
8677628 | Recursive file search using PowerShell | 357 | 2011-12-30 08:34:50 | <p>I am searching for a file in all the folders.</p>
<p><code>Copyforbuild.bat</code> is available in many places, and I would like to search recursively.</p>
<pre><code>$File = "V:\Myfolder\**\*.CopyForbuild.bat"
</code></pre>
<p>How can I do it in PowerShell?</p>
| 565,081 | 399,145 | 2023-02-11 08:39:42 | 8,677,658 | 550 | 2011-12-30 08:38:43 | 9,833 | 2017-06-20 18:25:15 | https://stackoverflow.com/q/8677628 | https://stackoverflow.com/a/8677658 | <p>Use the <a href="http://technet.microsoft.com/en-us/library/hh849800.aspx" rel="noreferrer">Get-ChildItem</a> cmdlet with the <code>-Recurse</code> switch:</p>
<pre><code>Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse -ErrorAction SilentlyContinue -Force
</code></pre>
| <p>Use the <a href="http://technet.microsoft.com/en-us/library/hh849800.aspx" rel="noreferrer">Get-ChildItem</a> cmdlet with the <code>-Recurse</code> switch:</p> <pre><code>Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse -ErrorAction SilentlyContinue -Force </code></pre> | 526, 24067 | powershell, powershell-2.0 | <h1>Recursive file search using PowerShell</h1>
<p>I am searching for a file in all the folders.</p>
<p><code>Copyforbuild.bat</code> is available in many places, and I would like to search recursively.</p>
<pre><code>$File = "V:\Myfolder\**\*.CopyForbuild.bat"
</code></pre>
<p>How can I do it in PowerShell?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 331 | bash | # Recursive file search using PowerShell
I am searching for a file in all the folders.
`Copyforbuild.bat` is available in many places, and I would like to search recursively.
```
$File = "V:\Myfolder\**\*.CopyForbuild.bat"
```
How can I do it in PowerShell? | Use the [Get-ChildItem](http://technet.microsoft.com/en-us/library/hh849800.aspx) cmdlet with the `-Recurse` switch:
```
Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse -ErrorAction SilentlyContinue -Force
``` |
3005963 | How can I have a newline in a string in sh? | 517 | 2010-06-09 13:00:21 | <p>This</p>
<pre><code>STR="Hello\nWorld"
echo $STR
</code></pre>
<p>produces as output</p>
<pre><code>Hello\nWorld
</code></pre>
<p>instead of</p>
<pre><code>Hello
World
</code></pre>
<p>What should I do to have a newline in a string?</p>
<p>Note: This question is not about <em>echo</em>.
I'm aware of <code>... | 528,804 | 359,178 | 2025-05-27 16:12:20 | 3,182,519 | 549 | 2010-07-05 22:43:03 | 237,955 | 2022-11-01 18:40:54 | https://stackoverflow.com/q/3005963 | https://stackoverflow.com/a/3182519 | <p>If you're using Bash, you can use backslash-escapes inside of a specially-quoted <code>$'string'</code>. For example, adding <code>\n</code>:</p>
<pre class="lang-bash prettyprint-override"><code>STR=$'Hello\nWorld'
echo "$STR" # quotes are required here!
</code></pre>
<p>Prints:</p>
<pre><code>Hello
World... | <p>If you're using Bash, you can use backslash-escapes inside of a specially-quoted <code>$'string'</code>. For example, adding <code>\n</code>:</p> <pre class="lang-bash prettyprint-override"><code>STR=$'Hello\nWorld' echo "$STR" # quotes are required here! </code></pre> <p>Prints:</p> <pre><code>Hello World... | 10327 | sh | <h1>How can I have a newline in a string in sh?</h1>
<p>This</p>
<pre><code>STR="Hello\nWorld"
echo $STR
</code></pre>
<p>produces as output</p>
<pre><code>Hello\nWorld
</code></pre>
<p>instead of</p>
<pre><code>Hello
World
</code></pre>
<p>What should I do to have a newline in a string?</p>
<p>Note: This que... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 332 | bash | # How can I have a newline in a string in sh?
This
```
STR="Hello\nWorld"
echo $STR
```
produces as output
```
Hello\nWorld
```
instead of
```
Hello
World
```
What should I do to have a newline in a string?
Note: This question is not about *echo*.
I'm aware of `echo -e`, but I'm looking for a solution that allo... | If you're using Bash, you can use backslash-escapes inside of a specially-quoted `$'string'`. For example, adding `\n`:
```
STR=$'Hello\nWorld'
echo "$STR" # quotes are required here!
```
Prints:
```
Hello
World
```
If you're using pretty much any other shell, just insert the newline as-is in the string:
```
STR='... |
16365130 | What is the difference between "#!/usr/bin/env bash" and "#!/usr/bin/bash"? | 701 | 2013-05-03 18:10:24 | <p>In the header of a Bash script, what's the difference between those two statements:</p>
<ol>
<li><p><code>#!/usr/bin/env bash</code></p></li>
<li><p><code>#!/usr/bin/bash</code></p></li>
</ol>
<p>When I consulted the <code>env</code> <a href="https://linux.die.net/man/1/env" rel="noreferrer">man page</a>, I get th... | 337,433 | 1,120,221 | 2025-12-27 17:59:18 | 16,365,367 | 547 | 2013-05-03 18:26:41 | 2,324,890 | 2018-12-29 14:12:13 | https://stackoverflow.com/q/16365130 | https://stackoverflow.com/a/16365367 | <p>Running a command through <code>/usr/bin/env</code> has the benefit of looking for whatever the default version of the program is in your current <strong>env</strong>ironment.</p>
<p>This way, you don't have to look for it in a specific place on the system, as those paths may be in different locations on different ... | <p>Running a command through <code>/usr/bin/env</code> has the benefit of looking for whatever the default version of the program is in your current <strong>env</strong>ironment.</p> <p>This way, you don't have to look for it in a specific place on the system, as those paths may be in different locations on different ... | 34, 58, 387, 390, 8780 | bash, linux, shebang, shell, unix | <h1>What is the difference between "#!/usr/bin/env bash" and "#!/usr/bin/bash"?</h1>
<p>In the header of a Bash script, what's the difference between those two statements:</p>
<ol>
<li><p><code>#!/usr/bin/env bash</code></p></li>
<li><p><code>#!/usr/bin/bash</code></p></li>
</ol>
<p>When I consulted the <code>env</co... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 333 | bash | # What is the difference between "#!/usr/bin/env bash" and "#!/usr/bin/bash"?
In the header of a Bash script, what's the difference between those two statements:
1. `#!/usr/bin/env bash`
2. `#!/usr/bin/bash`
When I consulted the `env` [man page](https://linux.die.net/man/1/env), I get this definition:
```
env - ru... | Running a command through `/usr/bin/env` has the benefit of looking for whatever the default version of the program is in your current **env**ironment.
This way, you don't have to look for it in a specific place on the system, as those paths may be in different locations on different systems. As long as it's in your p... |
2019989 | How to assign the output of a command to a Makefile variable | 400 | 2010-01-07 11:47:10 | <p>I need to execute some make rules conditionally, only if the Python installed is greater than a certain version (say 2.5).</p>
<p>I thought I could do something like executing:</p>
<pre><code>python -c 'import sys; print int(sys.version_info >= (2,5))'
</code></pre>
<p>and then using the output ('1' if ok, '0'... | 353,188 | 106,979 | 2025-05-18 08:40:47 | 2,020,006 | 546 | 2010-01-07 11:49:26 | 78,667 | 2018-07-19 01:32:47 | https://stackoverflow.com/q/2019989 | https://stackoverflow.com/a/2020006 | <p>Use the Make <code>shell</code> builtin like in <code>MY_VAR=$(shell echo whatever)</code></p>
<pre><code>me@Zack:~$make
MY_VAR IS whatever
</code></pre>
<p><p></p>
<pre><code>me@Zack:~$ cat Makefile
MY_VAR := $(shell echo whatever)
all:
@echo MY_VAR IS $(MY_VAR)
</code></pre>
| <p>Use the Make <code>shell</code> builtin like in <code>MY_VAR=$(shell echo whatever)</code></p> <pre><code>me@Zack:~$make MY_VAR IS whatever </code></pre> <p><p></p> <pre><code>me@Zack:~$ cat Makefile MY_VAR := $(shell echo whatever) all: @echo MY_VAR IS $(MY_VAR) </code></pre> | 390, 4301 | makefile, shell | <h1>How to assign the output of a command to a Makefile variable</h1>
<p>I need to execute some make rules conditionally, only if the Python installed is greater than a certain version (say 2.5).</p>
<p>I thought I could do something like executing:</p>
<pre><code>python -c 'import sys; print int(sys.version_info >... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 334 | bash | # How to assign the output of a command to a Makefile variable
I need to execute some make rules conditionally, only if the Python installed is greater than a certain version (say 2.5).
I thought I could do something like executing:
```
python -c 'import sys; print int(sys.version_info >= (2,5))'
```
and then using... | Use the Make `shell` builtin like in `MY_VAR=$(shell echo whatever)`
```
me@Zack:~$make
MY_VAR IS whatever
```
```
me@Zack:~$ cat Makefile
MY_VAR := $(shell echo whatever)
all:
@echo MY_VAR IS $(MY_VAR)
``` |
10969953 | How to output a multiline string in Bash? | 474 | 2012-06-10 15:27:16 | <p>How can I output a multipline string in Bash without using multiple echo calls like so:</p>
<pre><code>echo "usage: up [--level <n>| -n <levels>][--help][--version]"
echo
echo "Report bugs to: "
echo "up home page: "
</code></pre>
<p>I'm looking for a portable way to do this, using only Bash builtins.... | 615,886 | 1,178,669 | 2024-04-04 19:03:13 | 10,970,616 | 545 | 2012-06-10 17:11:44 | 26,428 | 2012-06-10 17:11:44 | https://stackoverflow.com/q/10969953 | https://stackoverflow.com/a/10970616 | <p>Here documents are often used for this purpose.</p>
<pre><code>cat << EOF
usage: up [--level <n>| -n <levels>][--help][--version]
Report bugs to:
up home page:
EOF
</code></pre>
<p>They are supported in all Bourne-derived shells including all versions of Bash.</p>
| <p>Here documents are often used for this purpose.</p> <pre><code>cat << EOF usage: up [--level <n>| -n <levels>][--help][--version] Report bugs to: up home page: EOF </code></pre> <p>They are supported in all Bourne-derived shells including all versions of Bash.</p> | 387 | bash | <h1>How to output a multiline string in Bash?</h1>
<p>How can I output a multipline string in Bash without using multiple echo calls like so:</p>
<pre><code>echo "usage: up [--level <n>| -n <levels>][--help][--version]"
echo
echo "Report bugs to: "
echo "up home page: "
</code></pre>
<p>I'm looking for a... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 335 | bash | # How to output a multiline string in Bash?
How can I output a multipline string in Bash without using multiple echo calls like so:
```
echo "usage: up [--level <n>| -n <levels>][--help][--version]"
echo
echo "Report bugs to: "
echo "up home page: "
```
I'm looking for a portable way to do this, using only Bash bui... | Here documents are often used for this purpose.
```
cat << EOF
usage: up [--level <n>| -n <levels>][--help][--version]
Report bugs to:
up home page:
EOF
```
They are supported in all Bourne-derived shells including all versions of Bash. |
4549489 | Can I change the name of `nohup.out`? | 328 | 2010-12-28 21:03:31 | <p>When I run <code>nohup some_command &</code>, the output goes to <code>nohup.out</code>; <code>man nohup</code> says to look at <code>info nohup</code> which in turn says:</p>
<blockquote>
<p>If standard output is a terminal, the
command's standard output is appended
to the file 'nohup.out'; if that
can... | 288,267 | 199,217 | 2023-09-12 15:45:11 | 4,549,515 | 545 | 2010-12-28 21:06:01 | 35,060 | 2020-02-14 07:54:39 | https://stackoverflow.com/q/4549489 | https://stackoverflow.com/a/4549515 | <pre><code>nohup some_command &> nohup2.out &
</code></pre>
<p>and voila.</p>
<hr>
<p>Older syntax for Bash version < 4:</p>
<pre><code>nohup some_command > nohup2.out 2>&1 &
</code></pre>
| <pre><code>nohup some_command &> nohup2.out & </code></pre> <p>and voila.</p> <hr> <p>Older syntax for Bash version < 4:</p> <pre><code>nohup some_command > nohup2.out 2>&1 & </code></pre> | 387, 942, 18966 | bash, logging, nohup | <h1>Can I change the name of `nohup.out`?</h1>
<p>When I run <code>nohup some_command &</code>, the output goes to <code>nohup.out</code>; <code>man nohup</code> says to look at <code>info nohup</code> which in turn says:</p>
<blockquote>
<p>If standard output is a terminal, the
command's standard output is ap... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 336 | bash | # Can I change the name of `nohup.out`?
When I run `nohup some_command &`, the output goes to `nohup.out`; `man nohup` says to look at `info nohup` which in turn says:
> If standard output is a terminal, the
> command's standard output is appended
> to the file 'nohup.out'; if that
> cannot be written to, it is appen... | ```
nohup some_command &> nohup2.out &
```
and voila.
---
Older syntax for Bash version < 4:
```
nohup some_command > nohup2.out 2>&1 &
``` |
14612371 | How do I run multiple background commands in bash in a single line? | 308 | 2013-01-30 19:45:38 | <p>I normally run multiple commands with something like this:</p>
<pre><code>sleep 2 && sleep 3
</code></pre>
<p>or</p>
<pre><code>sleep 2 ; sleep 3
</code></pre>
<p>but what if I want to run them both in the background from one command line command?</p>
<pre><code>sleep 2 & && sleep 3 &
</... | 135,915 | 970,171 | 2023-11-23 10:22:14 | 14,612,605 | 545 | 2013-01-30 20:00:39 | 141,131 | 2022-05-30 14:15:24 | https://stackoverflow.com/q/14612371 | https://stackoverflow.com/a/14612605 | <p>Exactly how do you want them to run? If you want them to be started in the <em>background</em> and run <strong>sequentially</strong>, you would do something like this:</p>
<pre><code>{ sleep 2; sleep 3; } &
</code></pre>
<p>If you want <code>sleep 3</code> to run only if <code>sleep 2</code> succeeds, then:</p>
... | <p>Exactly how do you want them to run? If you want them to be started in the <em>background</em> and run <strong>sequentially</strong>, you would do something like this:</p> <pre><code>{ sleep 2; sleep 3; } & </code></pre> <p>If you want <code>sleep 3</code> to run only if <code>sleep 2</code> succeeds, then:</p> ... | 58, 387, 390 | bash, linux, shell | <h1>How do I run multiple background commands in bash in a single line?</h1>
<p>I normally run multiple commands with something like this:</p>
<pre><code>sleep 2 && sleep 3
</code></pre>
<p>or</p>
<pre><code>sleep 2 ; sleep 3
</code></pre>
<p>but what if I want to run them both in the background from one co... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 337 | bash | # How do I run multiple background commands in bash in a single line?
I normally run multiple commands with something like this:
```
sleep 2 && sleep 3
```
or
```
sleep 2 ; sleep 3
```
but what if I want to run them both in the background from one command line command?
```
sleep 2 & && sleep 3 &
```
doesn't work... | Exactly how do you want them to run? If you want them to be started in the *background* and run **sequentially**, you would do something like this:
```
{ sleep 2; sleep 3; } &
```
If you want `sleep 3` to run only if `sleep 2` succeeds, then:
```
sleep 2 && sleep 3 &
```
If, on the other hand, you would like them t... |
2935183 | Bash: infinite sleep (infinite blocking) | 254 | 2010-05-29 13:12:14 | <p>I use <code>startx</code> to start X which will evaluate my <code>.xinitrc</code>. In my <code>.xinitrc</code> I start my window manager using <code>/usr/bin/mywm</code>. Now, if I kill my WM (in order to f.e. test some other WM), X will terminate too because the <code>.xinitrc</code> script reached EOF.
So I added ... | 240,910 | 239,117 | 2024-06-15 03:25:12 | 22,100,106 | 545 | 2014-02-28 16:07:57 | 2,319,964 | 2023-12-11 00:04:55 | https://stackoverflow.com/q/2935183 | https://stackoverflow.com/a/22100106 | <p><code>sleep infinity</code>, if implemented, will either sleep forever or sleep for the maximum sleep length, depending on the implementation. (see other answers and comments for this question that mention some of the variations)</p>
| <p><code>sleep infinity</code>, if implemented, will either sleep forever or sleep for the maximum sleep length, depending on the implementation. (see other answers and comments for this question that mention some of the variations)</p> | 58, 387, 2221, 13885 | bash, infinite, linux, sleep | <h1>Bash: infinite sleep (infinite blocking)</h1>
<p>I use <code>startx</code> to start X which will evaluate my <code>.xinitrc</code>. In my <code>.xinitrc</code> I start my window manager using <code>/usr/bin/mywm</code>. Now, if I kill my WM (in order to f.e. test some other WM), X will terminate too because the <co... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 338 | bash | # Bash: infinite sleep (infinite blocking)
I use `startx` to start X which will evaluate my `.xinitrc`. In my `.xinitrc` I start my window manager using `/usr/bin/mywm`. Now, if I kill my WM (in order to f.e. test some other WM), X will terminate too because the `.xinitrc` script reached EOF.
So I added this at the en... | `sleep infinity`, if implemented, will either sleep forever or sleep for the maximum sleep length, depending on the implementation. (see other answers and comments for this question that mention some of the variations) |
911168 | How can I detect if my shell script is running through a pipe? | 360 | 2009-05-26 15:03:29 | <p>How do I detect from within a shell script if its standard output is being sent to a terminal or if it's piped to another process?</p>
<p>The case in point: I'd like to add escape codes to colorize output, but only when run interactively, but not when piped, similar to what <code>ls --color</code> does.</p>
| 109,837 | null | 2024-10-24 07:36:59 | 911,213 | 544 | 2009-05-26 15:13:32 | 2,509 | 2021-11-28 14:53:05 | https://stackoverflow.com/q/911168 | https://stackoverflow.com/a/911213 | <p>In a pure POSIX shell,</p>
<pre><code>if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi
</code></pre>
<p>returns "terminal", because the output is sent to your terminal, whereas</p>
<pre><code>(if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi) | cat
</code... | <p>In a pure POSIX shell,</p> <pre><code>if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi </code></pre> <p>returns "terminal", because the output is sent to your terminal, whereas</p> <pre><code>(if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi) | cat </code... | 387, 390, 5813 | bash, pipe, shell | <h1>How can I detect if my shell script is running through a pipe?</h1>
<p>How do I detect from within a shell script if its standard output is being sent to a terminal or if it's piped to another process?</p>
<p>The case in point: I'd like to add escape codes to colorize output, but only when run interactively, but no... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 339 | bash | # How can I detect if my shell script is running through a pipe?
How do I detect from within a shell script if its standard output is being sent to a terminal or if it's piped to another process?
The case in point: I'd like to add escape codes to colorize output, but only when run interactively, but not when piped, s... | In a pure POSIX shell,
```
if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi
```
returns "terminal", because the output is sent to your terminal, whereas
```
(if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi) | cat
```
returns "not a terminal", because the output of the parenthetic el... |
9366816 | sed fails with "unknown option to `s'" error | 255 | 2012-02-20 18:58:50 | <p>I'm trying to use </p>
<pre><code>sed -i -e "s/.*seb.*/ \"$ftp_login_template\"/" $ftp_dir
</code></pre>
<p>however I get this error:</p>
<pre><code>sed: -e expression #1, char 34: unknown option to `s'
</code></pre>
<p>I don't understand why since this works perfectly:</p>
<pre><code>sed -i -e "s/.*wbspassw... | 197,220 | 1,015,761 | 2023-09-04 09:50:32 | 9,366,940 | 543 | 2012-02-20 19:08:48 | 882,600 | 2020-04-22 09:26:42 | https://stackoverflow.com/q/9366816 | https://stackoverflow.com/a/9366940 | <p>The problem is with slashes: your variable contains them and the final command will be something like <code>sed "s/string/path/to/something/g"</code>, containing way too many slashes.</p>
<p>Since <code>sed</code> can take any char as delimiter (without having to declare the new delimiter), you can try using anothe... | <p>The problem is with slashes: your variable contains them and the final command will be something like <code>sed "s/string/path/to/something/g"</code>, containing way too many slashes.</p> <p>Since <code>sed</code> can take any char as delimiter (without having to declare the new delimiter), you can try using anothe... | 387, 5282, 10327 | bash, sed, sh | <h1>sed fails with "unknown option to `s'" error</h1>
<p>I'm trying to use </p>
<pre><code>sed -i -e "s/.*seb.*/ \"$ftp_login_template\"/" $ftp_dir
</code></pre>
<p>however I get this error:</p>
<pre><code>sed: -e expression #1, char 34: unknown option to `s'
</code></pre>
<p>I don't understand why since this wo... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 340 | bash | # sed fails with "unknown option to `s'" error
I'm trying to use
```
sed -i -e "s/.*seb.*/ \"$ftp_login_template\"/" $ftp_dir
```
however I get this error:
```
sed: -e expression #1, char 34: unknown option to `s'
```
I don't understand why since this works perfectly:
```
sed -i -e "s/.*wbspassword.*/ \"wbs... | The problem is with slashes: your variable contains them and the final command will be something like `sed "s/string/path/to/something/g"`, containing way too many slashes.
Since `sed` can take any char as delimiter (without having to declare the new delimiter), you can try using another one that doesn't appear in you... |
589276 | How can I use Bash syntax in Makefile targets? | 292 | 2009-02-26 05:56:16 | <p>I often find <a href="http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29" rel="noreferrer">Bash</a> syntax very helpful, e.g. process substitution like in <code>diff <(sort file1) <(sort file2)</code>.</p>
<p>Is it possible to use such Bash commands in a Makefile? I'm thinking of something like this:</p>
<pre>... | 228,726 | 60,628 | 2023-03-23 15:03:33 | 589,300 | 541 | 2009-02-26 06:04:21 | 27,727 | 2022-06-22 21:50:51 | https://stackoverflow.com/q/589276 | https://stackoverflow.com/a/589300 | <p>From the <a href="https://www.gnu.org/software/make/manual/html_node/Choosing-the-Shell.html" rel="noreferrer">GNU Make documentation</a>,</p>
<pre class="lang-none prettyprint-override"><code>5.3.2 Choosing the Shell
------------------------
The program used as the shell is taken from the variable `SHELL'. If
thi... | <p>From the <a href="https://www.gnu.org/software/make/manual/html_node/Choosing-the-Shell.html" rel="noreferrer">GNU Make documentation</a>,</p> <pre class="lang-none prettyprint-override"><code>5.3.2 Choosing the Shell ------------------------ The program used as the shell is taken from the variable `SHELL'. If thi... | 387, 390, 4301 | bash, makefile, shell | <h1>How can I use Bash syntax in Makefile targets?</h1>
<p>I often find <a href="http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29" rel="noreferrer">Bash</a> syntax very helpful, e.g. process substitution like in <code>diff <(sort file1) <(sort file2)</code>.</p>
<p>Is it possible to use such Bash commands in a ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 341 | bash | # How can I use Bash syntax in Makefile targets?
I often find [Bash](http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) syntax very helpful, e.g. process substitution like in `diff <(sort file1) <(sort file2)`.
Is it possible to use such Bash commands in a Makefile? I'm thinking of something like this:
```
file-dif... | From the [GNU Make documentation](https://www.gnu.org/software/make/manual/html_node/Choosing-the-Shell.html),
```
5.3.2 Choosing the Shell
------------------------
The program used as the shell is taken from the variable `SHELL'. If
this variable is not set in your makefile, the program `/bin/sh' is
used as the she... |
380817 | Best way to simulate "group by" from bash? | 319 | 2008-12-19 12:13:58 | <p>Suppose you have a file that contains IP addresses, one address in each line:</p>
<pre><code>10.0.10.1
10.0.10.1
10.0.10.3
10.0.10.2
10.0.10.1
</code></pre>
<p>You need a shell script that counts for each IP address how many times it appears in the file. For the previous input you need the following output:</p>
<... | 226,349 | 686 | 2023-01-12 12:20:18 | 380,832 | 539 | 2008-12-19 12:22:35 | 40,342 | 2008-12-19 12:22:35 | https://stackoverflow.com/q/380817 | https://stackoverflow.com/a/380832 | <pre><code>sort ip_addresses | uniq -c
</code></pre>
<p>This will print the count first, but other than that it should be exactly what you want.</p>
| <pre><code>sort ip_addresses | uniq -c </code></pre> <p>This will print the count first, but other than that it should be exactly what you want.</p> | 387, 531 | bash, scripting | <h1>Best way to simulate "group by" from bash?</h1>
<p>Suppose you have a file that contains IP addresses, one address in each line:</p>
<pre><code>10.0.10.1
10.0.10.1
10.0.10.3
10.0.10.2
10.0.10.1
</code></pre>
<p>You need a shell script that counts for each IP address how many times it appears in the file. For the ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 342 | bash | # Best way to simulate "group by" from bash?
Suppose you have a file that contains IP addresses, one address in each line:
```
10.0.10.1
10.0.10.1
10.0.10.3
10.0.10.2
10.0.10.1
```
You need a shell script that counts for each IP address how many times it appears in the file. For the previous input you need the follo... | ```
sort ip_addresses | uniq -c
```
This will print the count first, but other than that it should be exactly what you want. |
13489398 | Delete files older than 10 days using shell script in Unix | 174 | 2012-11-21 08:46:13 | <p>I want to delete scripts in a folder from the current date back to 10 days.
The scripts looks like:</p>
<pre><code>2012.11.21.09_33_52.script
2012.11.21.09_33_56.script
2012.11.21.09_33_59.script
</code></pre>
<p>The script will run in every 10 day with Crontab, that's why I need the current date.</p>
| 410,963 | 1,781,022 | 2022-12-29 03:22:54 | 13,489,511 | 539 | 2012-11-21 08:54:10 | 465,183 | 2016-10-10 18:01:32 | https://stackoverflow.com/q/13489398 | https://stackoverflow.com/a/13489511 | <p><code>find</code> is the common tool for this kind of task :</p>
<pre><code>find ./my_dir -mtime +10 -type f -delete
</code></pre>
<p><strong>EXPLANATIONS</strong></p>
<ul>
<li><code>./my_dir</code> your directory (replace with your own)</li>
<li><code>-mtime +10</code> older than 10 days</li>
<li><code>-type f</... | <p><code>find</code> is the common tool for this kind of task :</p> <pre><code>find ./my_dir -mtime +10 -type f -delete </code></pre> <p><strong>EXPLANATIONS</strong></p> <ul> <li><code>./my_dir</code> your directory (replace with your own)</li> <li><code>-mtime +10</code> older than 10 days</li> <li><code>-type f</... | 34, 387, 390, 10193 | bash, find, shell, unix | <h1>Delete files older than 10 days using shell script in Unix</h1>
<p>I want to delete scripts in a folder from the current date back to 10 days.
The scripts looks like:</p>
<pre><code>2012.11.21.09_33_52.script
2012.11.21.09_33_56.script
2012.11.21.09_33_59.script
</code></pre>
<p>The script will run in every 10 day ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 343 | bash | # Delete files older than 10 days using shell script in Unix
I want to delete scripts in a folder from the current date back to 10 days.
The scripts looks like:
```
2012.11.21.09_33_52.script
2012.11.21.09_33_56.script
2012.11.21.09_33_59.script
```
The script will run in every 10 day with Crontab, that's why I need... | `find` is the common tool for this kind of task :
```
find ./my_dir -mtime +10 -type f -delete
```
**EXPLANATIONS**
- `./my_dir` your directory (replace with your own)
- `-mtime +10` older than 10 days
- `-type f` only files
- `-delete` no surprise. **Remove it to test your `find` filter before executing the whole c... |
16618071 | Can I export a variable to the environment from a Bash script without sourcing it? | 497 | 2013-05-17 21:18:02 | <p>Suppose that I have this script:</p>
<p><strong>export.bash</strong>:</p>
<pre><code>#! /usr/bin/env bash
export VAR="HELLO, VARIABLE"
</code></pre>
<p>When I execute the script and try to access to the <code>$VAR</code>, I don't get any value!</p>
<pre><code>echo $VAR
</code></pre>
<p>Is there a way to ac... | 583,932 | 1,120,221 | 2024-10-08 05:50:20 | 16,619,261 | 538 | 2013-05-17 23:36:24 | 827,263 | 2019-03-25 20:48:51 | https://stackoverflow.com/q/16618071 | https://stackoverflow.com/a/16619261 | <blockquote>
<p>Is there any way to access to the <code>$VAR</code> by just executing <code>export.bash</code> without sourcing it ?</p>
</blockquote>
<p>Quick answer: No.</p>
<p>But there are several possible workarounds.</p>
<p>The most obvious one, which you've already mentioned, is to use <code>source</code> o... | <blockquote> <p>Is there any way to access to the <code>$VAR</code> by just executing <code>export.bash</code> without sourcing it ?</p> </blockquote> <p>Quick answer: No.</p> <p>But there are several possible workarounds.</p> <p>The most obvious one, which you've already mentioned, is to use <code>source</code> o... | 34, 387, 390, 9013 | bash, environment-variables, shell, unix | <h1>Can I export a variable to the environment from a Bash script without sourcing it?</h1>
<p>Suppose that I have this script:</p>
<p><strong>export.bash</strong>:</p>
<pre><code>#! /usr/bin/env bash
export VAR="HELLO, VARIABLE"
</code></pre>
<p>When I execute the script and try to access to the <code>$VAR</... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 344 | bash | # Can I export a variable to the environment from a Bash script without sourcing it?
Suppose that I have this script:
**export.bash**:
```
#! /usr/bin/env bash
export VAR="HELLO, VARIABLE"
```
When I execute the script and try to access to the `$VAR`, I don't get any value!
```
echo $VAR
```
Is there a way to acc... | > Is there any way to access to the `$VAR` by just executing `export.bash` without sourcing it ?
Quick answer: No.
But there are several possible workarounds.
The most obvious one, which you've already mentioned, is to use `source` or `.` to execute the script in the context of the calling shell:
```
$ cat set-vars... |
8920245 | Bash conditionals: how to "and" expressions? (if [ ! -z $VAR && -e $VAR ]) | 286 | 2012-01-19 02:13:08 | <p>I guess I'm not clear on how to do "and" tests. I wanted to make sure an argument existed which was working well with <code>[ -e $VAR ]</code>, but it turns out that was also evaluating as true on an empty string; which I do not want.</p>
<p>How do I 'and' them together? Or is there another unary test that accompli... | 291,345 | 1,123,802 | 2020-01-01 00:45:58 | 8,920,266 | 538 | 2012-01-19 02:16:06 | 970,195 | 2014-12-22 22:36:15 | https://stackoverflow.com/q/8920245 | https://stackoverflow.com/a/8920266 | <pre><code>if [ ! -z "$var" ] && [ -e "$var" ]; then
# something ...
fi
</code></pre>
| <pre><code>if [ ! -z "$var" ] && [ -e "$var" ]; then # something ... fi </code></pre> | 387, 390 | bash, shell | <h1>Bash conditionals: how to "and" expressions? (if [ ! -z $VAR && -e $VAR ])</h1>
<p>I guess I'm not clear on how to do "and" tests. I wanted to make sure an argument existed which was working well with <code>[ -e $VAR ]</code>, but it turns out that was also evaluating as true on an empty string; which I do not want... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 345 | bash | # Bash conditionals: how to "and" expressions? (if [ ! -z $VAR && -e $VAR ])
I guess I'm not clear on how to do "and" tests. I wanted to make sure an argument existed which was working well with `[ -e $VAR ]`, but it turns out that was also evaluating as true on an empty string; which I do not want.
How do I 'and' th... | ```
if [ ! -z "$var" ] && [ -e "$var" ]; then
# something ...
fi
``` |
8955425 | How can I convert a series of images to a PDF from the command line on Linux? | 344 | 2012-01-21 18:18:09 | <p>I have a scanning server I wrote in CGI and Bash. I want to be able to convert a bunch of images (all in one folder) to a PDF from the command line. How can that be done?</p>
| 239,912 | 931,721 | 2025-04-28 07:48:00 | 8,955,465 | 537 | 2012-01-21 18:22:43 | 1,101,070 | 2022-09-24 20:05:34 | https://stackoverflow.com/q/8955425 | https://stackoverflow.com/a/8955465 | <p>Using <a href="https://www.imagemagick.org/script/index.php" rel="noreferrer">ImageMagick</a>, you can try:</p>
<pre><code>convert page.png page.pdf
</code></pre>
<p>For multiple images:</p>
<pre><code>convert page*.png mydoc.pdf
</code></pre>
| <p>Using <a href="https://www.imagemagick.org/script/index.php" rel="noreferrer">ImageMagick</a>, you can try:</p> <pre><code>convert page.png page.pdf </code></pre> <p>For multiple images:</p> <pre><code>convert page*.png mydoc.pdf </code></pre> | 90, 387, 4985 | bash, image, pdf | <h1>How can I convert a series of images to a PDF from the command line on Linux?</h1>
<p>I have a scanning server I wrote in CGI and Bash. I want to be able to convert a bunch of images (all in one folder) to a PDF from the command line. How can that be done?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 346 | bash | # How can I convert a series of images to a PDF from the command line on Linux?
I have a scanning server I wrote in CGI and Bash. I want to be able to convert a bunch of images (all in one folder) to a PDF from the command line. How can that be done? | Using [ImageMagick](https://www.imagemagick.org/script/index.php), you can try:
```
convert page.png page.pdf
```
For multiple images:
```
convert page*.png mydoc.pdf
``` |
6442364 | Running script upon login in mac OS X | 372 | 2011-06-22 15:23:50 | <p>I am wondering if anyone is able to help me out with getting a shell (.sh) program to automatically run whenever I log in to my account on my computer. I am running Mac OS X 10.6.7.</p>
<p>I have a file "Example.sh" that I want to run when I log onto my computer. I do not have a problem running it when I a... | 376,515 | 700,338 | 2022-06-22 23:26:25 | 6,445,525 | 536 | 2011-06-22 19:39:57 | 632,407 | 2022-06-22 23:26:25 | https://stackoverflow.com/q/6442364 | https://stackoverflow.com/a/6445525 | <p>Follow this:</p>
<ul>
<li><p>start <code>Automator.app</code></p>
</li>
<li><p>select <code>Application</code></p>
</li>
<li><p>click <code>Show library</code> in the toolbar (if hidden)</p>
</li>
<li><p>add <code>Run shell script</code> (from the <code>Actions/Utilities</code>)</p>
</li>
<li><p>copy & paste you... | <p>Follow this:</p> <ul> <li><p>start <code>Automator.app</code></p> </li> <li><p>select <code>Application</code></p> </li> <li><p>click <code>Show library</code> in the toolbar (if hidden)</p> </li> <li><p>add <code>Run shell script</code> (from the <code>Actions/Utilities</code>)</p> </li> <li><p>copy & paste you... | 387, 390, 1362, 32395 | automation, bash, osx-snow-leopard, shell | <h1>Running script upon login in mac OS X</h1>
<p>I am wondering if anyone is able to help me out with getting a shell (.sh) program to automatically run whenever I log in to my account on my computer. I am running Mac OS X 10.6.7.</p>
<p>I have a file "Example.sh" that I want to run when I log onto my comput... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 347 | bash | # Running script upon login in mac OS X
I am wondering if anyone is able to help me out with getting a shell (.sh) program to automatically run whenever I log in to my account on my computer. I am running Mac OS X 10.6.7.
I have a file "Example.sh" that I want to run when I log onto my computer. I do not have a probl... | Follow this:
- start `Automator.app`
- select `Application`
- click `Show library` in the toolbar (if hidden)
- add `Run shell script` (from the `Actions/Utilities`)
- copy & paste your script into the window
- test it
- save somewhere (for example you can make an `Applications` folder in your HOME, you will get an `y... |
8296710 | How to ignore xargs commands if stdin input is empty? | 307 | 2011-11-28 13:46:53 | <p>Consider this command:</p>
<pre><code>ls /mydir/*.txt | xargs chown root
</code></pre>
<p>The intention is to change owners of all text files in <code>mydir</code> to root</p>
<p>The issue is that if there are no <code>.txt</code> files in <code>mydir</code> then xargs thows an error saying there is no path speci... | 80,149 | 157,837 | 2022-09-11 21:20:03 | 8,296,746 | 535 | 2011-11-28 13:49:10 | 279,627 | 2021-10-17 19:00:36 | https://stackoverflow.com/q/8296710 | https://stackoverflow.com/a/8296746 | <p>For GNU <code>xargs</code>, you can use the <code>-r</code> or <code>--no-run-if-empty</code> option:</p>
<blockquote>
<p><code>--no-run-if-empty</code> <br>
<code>-r</code> <br> If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no in... | <p>For GNU <code>xargs</code>, you can use the <code>-r</code> or <code>--no-run-if-empty</code> option:</p> <blockquote> <p><code>--no-run-if-empty</code> <br> <code>-r</code> <br> If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no in... | 387, 4956, 10326 | bash, centos, xargs | <h1>How to ignore xargs commands if stdin input is empty?</h1>
<p>Consider this command:</p>
<pre><code>ls /mydir/*.txt | xargs chown root
</code></pre>
<p>The intention is to change owners of all text files in <code>mydir</code> to root</p>
<p>The issue is that if there are no <code>.txt</code> files in <code>mydir... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 348 | bash | # How to ignore xargs commands if stdin input is empty?
Consider this command:
```
ls /mydir/*.txt | xargs chown root
```
The intention is to change owners of all text files in `mydir` to root
The issue is that if there are no `.txt` files in `mydir` then xargs thows an error saying there is no path specified. This... | For GNU `xargs`, you can use the `-r` or `--no-run-if-empty` option:
> `--no-run-if-empty`
> `-r`
> If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension.
The BSD version of `xargs`, which is use... |
5750450 | How can I print each command before executing? | 401 | 2011-04-21 22:27:23 | <p>What is the best way to set up a Bash script that prints each command before it executes it?</p>
<p>That would be great for debugging purposes.</p>
<p>I already tried this:</p>
<pre><code>CMD="./my-command --params >stdout.txt 2>stderr.txt"
echo $CMD
`$CMD`
</code></pre>
<p>It's supposed to print this firs... | 318,331 | 60,628 | 2019-04-15 00:21:49 | 5,750,463 | 534 | 2011-04-21 22:28:59 | 85,371 | 2011-04-21 22:28:59 | https://stackoverflow.com/q/5750450 | https://stackoverflow.com/a/5750463 | <pre><code>set -o xtrace
</code></pre>
<p>or</p>
<pre><code>bash -x myscript.sh
</code></pre>
<p><em>This works with standard /bin/sh as well IIRC (it might be a POSIX thing then)</em></p>
<p>And remember, there is <a href="http://bashdb.sourceforge.net" rel="noreferrer"><strong>bashdb</strong></a> (<code>bash Shel... | <pre><code>set -o xtrace </code></pre> <p>or</p> <pre><code>bash -x myscript.sh </code></pre> <p><em>This works with standard /bin/sh as well IIRC (it might be a POSIX thing then)</em></p> <p>And remember, there is <a href="http://bashdb.sourceforge.net" rel="noreferrer"><strong>bashdb</strong></a> (<code>bash Shel... | 387 | bash | <h1>How can I print each command before executing?</h1>
<p>What is the best way to set up a Bash script that prints each command before it executes it?</p>
<p>That would be great for debugging purposes.</p>
<p>I already tried this:</p>
<pre><code>CMD="./my-command --params >stdout.txt 2>stderr.txt"
echo $CMD
`... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 349 | bash | # How can I print each command before executing?
What is the best way to set up a Bash script that prints each command before it executes it?
That would be great for debugging purposes.
I already tried this:
```
CMD="./my-command --params >stdout.txt 2>stderr.txt"
echo $CMD
`$CMD`
```
It's supposed to print this f... | ```
set -o xtrace
```
or
```
bash -x myscript.sh
```
*This works with standard /bin/sh as well IIRC (it might be a POSIX thing then)*
And remember, there is [**bashdb**](http://bashdb.sourceforge.net) (`bash Shell Debugger, release 4.0-0.4`)
---
To revert to normal, exit the subshell or
```
set +o xtrace
``` |
20449543 | Shell equality operators (=, ==, -eq) | 399 | 2013-12-08 03:29:58 | <p>What is the difference between <code>=</code>, <code>==</code> and <code>-eq</code> in shell scripting?</p>
<p>Is there any difference between the following?</p>
<pre><code>[ $a = $b ]
[ $a == $b ]
[ $a -eq $b ]
</code></pre>
<p>Is it simply that <code>=</code> and <code>==</code> are only used when the variables co... | 652,131 | 2,845,038 | 2022-05-28 23:34:46 | 20,449,556 | 534 | 2013-12-08 03:31:34 | 68,587 | 2022-05-28 23:34:46 | https://stackoverflow.com/q/20449543 | https://stackoverflow.com/a/20449556 | <p><code>=</code> and <code>==</code> are for string comparisons<br />
<code>-eq</code> is for numeric comparisons<br />
<code>-eq</code> is in the same family as <code>-lt</code>, <code>-le</code>, <code>-gt</code>, <code>-ge</code>, and <code>-ne</code></p>
<p><code>==</code> is specific to bash (not present in sh (B... | <p><code>=</code> and <code>==</code> are for string comparisons<br /> <code>-eq</code> is for numeric comparisons<br /> <code>-eq</code> is in the same family as <code>-lt</code>, <code>-le</code>, <code>-gt</code>, <code>-ge</code>, and <code>-ne</code></p> <p><code>==</code> is specific to bash (not present in sh (B... | 387, 390 | bash, shell | <h1>Shell equality operators (=, ==, -eq)</h1>
<p>What is the difference between <code>=</code>, <code>==</code> and <code>-eq</code> in shell scripting?</p>
<p>Is there any difference between the following?</p>
<pre><code>[ $a = $b ]
[ $a == $b ]
[ $a -eq $b ]
</code></pre>
<p>Is it simply that <code>=</code> and <cod... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 350 | bash | # Shell equality operators (=, ==, -eq)
What is the difference between `=`, `==` and `-eq` in shell scripting?
Is there any difference between the following?
```
[ $a = $b ]
[ $a == $b ]
[ $a -eq $b ]
```
Is it simply that `=` and `==` are only used when the variables contain numbers? | `=` and `==` are for string comparisons
`-eq` is for numeric comparisons
`-eq` is in the same family as `-lt`, `-le`, `-gt`, `-ge`, and `-ne`
`==` is specific to bash (not present in sh (Bourne shell), ...). Using POSIX `=` is preferred for compatibility. In bash the two are equivalent, and in sh `=` is the only o... |
1731767 | How can I create nonexistent subdirectories recursively using Bash? | 239 | 2009-11-13 20:42:03 | <p>I am creating a quick backup script that will dump some databases into a nice/neat directory structure and I realized that I need to test to make sure that the directories exist before I create them. The code I have works, but is there a better way to do it?</p>
<pre><code>[ -d "$BACKUP_DIR" ] || mkdir &qu... | 145,884 | 85,309 | 2021-10-27 18:07:04 | 1,731,775 | 534 | 2009-11-13 20:42:53 | 131,433 | 2015-07-09 12:48:59 | https://stackoverflow.com/q/1731767 | https://stackoverflow.com/a/1731775 | <p>You can use the <code>-p</code> parameter, which is <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?mkdir" rel="noreferrer">documented as</a>:</p>
<blockquote>
<p>-p, --parents</p>
<p>no error if existing, make parent directories as needed</p>
</blockquote>
<p>So:</p>
<pre><code>mkdir -p "$BACKUP_DIR/$client/$yea... | <p>You can use the <code>-p</code> parameter, which is <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?mkdir" rel="noreferrer">documented as</a>:</p> <blockquote> <p>-p, --parents</p> <p>no error if existing, make parent directories as needed</p> </blockquote> <p>So:</p> <pre><code>mkdir -p "$BACKUP_DIR/$client/$yea... | 387, 390, 578, 34708 | bash, recursion, shell, subdirectory | <h1>How can I create nonexistent subdirectories recursively using Bash?</h1>
<p>I am creating a quick backup script that will dump some databases into a nice/neat directory structure and I realized that I need to test to make sure that the directories exist before I create them. The code I have works, but is there a be... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 351 | bash | # How can I create nonexistent subdirectories recursively using Bash?
I am creating a quick backup script that will dump some databases into a nice/neat directory structure and I realized that I need to test to make sure that the directories exist before I create them. The code I have works, but is there a better way ... | You can use the `-p` parameter, which is [documented as](http://unixhelp.ed.ac.uk/CGI/man-cgi?mkdir):
> -p, --parents
>
> no error if existing, make parent directories as needed
So:
```
mkdir -p "$BACKUP_DIR/$client/$year/$month/$day"
``` |
17385794 | How to get the process ID to kill a nohup process? | 320 | 2013-06-29 23:55:42 | <p>I'm running a nohup process on the server. When I try to kill it my putty console closes instead.</p>
<p>this is how I try to find the process ID:</p>
<pre><code>ps -ef |grep nohup
</code></pre>
<p>this is the command to kill</p>
<pre><code> kill -9 1787 787
</code></pre>
| 732,749 | 2,535,056 | 2023-05-21 08:08:08 | 17,389,526 | 533 | 2013-06-30 10:42:05 | 980,550 | 2020-09-03 21:51:20 | https://stackoverflow.com/q/17385794 | https://stackoverflow.com/a/17389526 | <p>When using <code>nohup</code> and you put the task in the background, the background operator (<code>&</code>) will give you the PID at the command prompt. If your plan is to manually manage the process, you can save that PID and use it later to kill the process if needed, via <code>kill PID</code> or <code>kill... | <p>When using <code>nohup</code> and you put the task in the background, the background operator (<code>&</code>) will give you the PID at the command prompt. If your plan is to manually manage the process, you can save that PID and use it later to kill the process if needed, via <code>kill PID</code> or <code>kill... | 58, 387, 1271, 18966 | bash, grep, linux, nohup | <h1>How to get the process ID to kill a nohup process?</h1>
<p>I'm running a nohup process on the server. When I try to kill it my putty console closes instead.</p>
<p>this is how I try to find the process ID:</p>
<pre><code>ps -ef |grep nohup
</code></pre>
<p>this is the command to kill</p>
<pre><code> kill -9 17... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 352 | bash | # How to get the process ID to kill a nohup process?
I'm running a nohup process on the server. When I try to kill it my putty console closes instead.
this is how I try to find the process ID:
```
ps -ef |grep nohup
```
this is the command to kill
```
kill -9 1787 787
``` | When using `nohup` and you put the task in the background, the background operator (`&`) will give you the PID at the command prompt. If your plan is to manually manage the process, you can save that PID and use it later to kill the process if needed, via `kill PID` or `kill -9 PID` (if you need to force kill). Alterna... |
2005192 | How to execute a bash command stored as a string with quotes and asterisk | 356 | 2010-01-05 09:55:04 | <p>I try to execute the following command :</p>
<pre><code>mysql AMORE -u username -ppassword -h localhost -e "SELECT host FROM amoreconfig"
</code></pre>
<p>I store it in a string :</p>
<pre><code>cmd="mysql AMORE -u username -ppassword -h localhost -e\"SELECT host FROM amoreconfig\""
</code></pre>
<p>Test it ... | 415,565 | 20,986 | 2018-04-06 17:18:12 | 2,005,201 | 532 | 2010-01-05 09:57:53 | 167,735 | 2010-01-05 15:27:22 | https://stackoverflow.com/q/2005192 | https://stackoverflow.com/a/2005201 | <p>Have you tried:</p>
<pre><code>eval $cmd
</code></pre>
<p>For the follow-on question of how to escape <code>*</code> since it has special meaning when it's naked or in double quoted strings: use single quotes.</p>
<pre><code>MYSQL='mysql AMORE -u username -ppassword -h localhost -e'
QUERY="SELECT "'*'" FROM amore... | <p>Have you tried:</p> <pre><code>eval $cmd </code></pre> <p>For the follow-on question of how to escape <code>*</code> since it has special meaning when it's naked or in double quoted strings: use single quotes.</p> <pre><code>MYSQL='mysql AMORE -u username -ppassword -h localhost -e' QUERY="SELECT "'*'" FROM amore... | 387, 430, 531, 4804 | bash, escaping, quotes, scripting | <h1>How to execute a bash command stored as a string with quotes and asterisk</h1>
<p>I try to execute the following command :</p>
<pre><code>mysql AMORE -u username -ppassword -h localhost -e "SELECT host FROM amoreconfig"
</code></pre>
<p>I store it in a string :</p>
<pre><code>cmd="mysql AMORE -u username -ppas... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 353 | bash | # How to execute a bash command stored as a string with quotes and asterisk
I try to execute the following command :
```
mysql AMORE -u username -ppassword -h localhost -e "SELECT host FROM amoreconfig"
```
I store it in a string :
```
cmd="mysql AMORE -u username -ppassword -h localhost -e\"SELECT host FROM am... | Have you tried:
```
eval $cmd
```
For the follow-on question of how to escape `*` since it has special meaning when it's naked or in double quoted strings: use single quotes.
```
MYSQL='mysql AMORE -u username -ppassword -h localhost -e'
QUERY="SELECT "'*'" FROM amoreconfig" ;# <-- "double"'single'"double"
eval $MYS... |
15559359 | Insert line after match using sed | 366 | 2013-03-21 22:23:35 | <p>How would I go about inserting a choice line of text after the first line matching a specific string using the <code>sed</code> command. I have ...</p>
<pre><code>CLIENTSCRIPT="foo"
CLIENTFILE="bar"
</code></pre>
<p>And I want insert a line after the <code>CLIENTSCRIPT=</code> line resulting in ... | 447,856 | 2,150,250 | 2026-01-14 12:45:23 | 15,559,399 | 531 | 2013-03-21 22:27:07 | 465,183 | 2018-07-13 10:12:06 | https://stackoverflow.com/q/15559359 | https://stackoverflow.com/a/15559399 | <p>Try doing this using GNU sed:</p>
<pre><code>sed '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' file
</code></pre>
<p>if you want to substitute <em>in-place</em>, use</p>
<pre><code>sed -i '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' file
</code></pre>
<h2>Output</h2>
<pre><code>CLIENTSCRIPT="foo"
CLIENTSCRIPT2... | <p>Try doing this using GNU sed:</p> <pre><code>sed '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' file </code></pre> <p>if you want to substitute <em>in-place</em>, use</p> <pre><code>sed -i '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' file </code></pre> <h2>Output</h2> <pre><code>CLIENTSCRIPT="foo" CLIENTSCRIPT2... | 390, 5282 | sed, shell | <h1>Insert line after match using sed</h1>
<p>How would I go about inserting a choice line of text after the first line matching a specific string using the <code>sed</code> command. I have ...</p>
<pre><code>CLIENTSCRIPT="foo"
CLIENTFILE="bar"
</code></pre>
<p>And I want insert a line after the <c... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 354 | bash | # Insert line after match using sed
How would I go about inserting a choice line of text after the first line matching a specific string using the `sed` command. I have ...
```
CLIENTSCRIPT="foo"
CLIENTFILE="bar"
```
And I want insert a line after the `CLIENTSCRIPT=` line resulting in ...
```
CLIENTSCRIPT="foo"
CLI... | Try doing this using GNU sed:
```
sed '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' file
```
if you want to substitute *in-place*, use
```
sed -i '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' file
```
## Output
```
CLIENTSCRIPT="foo"
CLIENTSCRIPT2="hello"
CLIENTFILE="bar"
```
## Doc
- see [sed doc](http://www.gn... |
3294072 | Get last dirname/filename in a file path argument in Bash | 365 | 2010-07-20 20:25:50 | <p>I'm trying to write a post-commit hook for SVN, which is hosted on our development server. My goal is to try to automatically checkout a copy of the committed project to the directory where it is hosted on the server. However I need to be able to read only the last directory in the directory string passed to the s... | 447,628 | 12,605 | 2025-01-19 12:21:33 | 3,294,102 | 530 | 2010-07-20 20:29:44 | 56,338 | 2022-07-15 10:42:19 | https://stackoverflow.com/q/3294072 | https://stackoverflow.com/a/3294102 | <p><a href="https://linux.die.net/man/1/basename" rel="noreferrer"><code>basename</code></a> does remove the directory prefix of a path:</p>
<pre><code>$ basename /usr/local/svn/repos/example
example
$ echo "/server/root/$(basename /usr/local/svn/repos/example)"
/server/root/example
</code></pre>
| <p><a href="https://linux.die.net/man/1/basename" rel="noreferrer"><code>basename</code></a> does remove the directory prefix of a path:</p> <pre><code>$ basename /usr/local/svn/repos/example example $ echo "/server/root/$(basename /usr/local/svn/repos/example)" /server/root/example </code></pre> | 58, 63, 387, 390 | bash, linux, shell, svn | <h1>Get last dirname/filename in a file path argument in Bash</h1>
<p>I'm trying to write a post-commit hook for SVN, which is hosted on our development server. My goal is to try to automatically checkout a copy of the committed project to the directory where it is hosted on the server. However I need to be able to r... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 356 | bash | # Get last dirname/filename in a file path argument in Bash
I'm trying to write a post-commit hook for SVN, which is hosted on our development server. My goal is to try to automatically checkout a copy of the committed project to the directory where it is hosted on the server. However I need to be able to read only th... | [`basename`](https://linux.die.net/man/1/basename) does remove the directory prefix of a path:
```
$ basename /usr/local/svn/repos/example
example
$ echo "/server/root/$(basename /usr/local/svn/repos/example)"
/server/root/example
``` |
2762994 | Define an alias in fish shell | 304 | 2010-05-04 05:26:35 | <p>I would like to define some aliases in fish. Apparently it should be possible to define them in </p>
<pre><code>~/.config/fish/functions
</code></pre>
<p>but they don't get auto loaded when I restart the shell. Any ideas?</p>
| 250,793 | 45,112 | 2024-02-29 20:18:48 | 2,763,014 | 529 | 2010-05-04 05:32:29 | 291,550 | 2024-02-29 20:18:48 | https://stackoverflow.com/q/2762994 | https://stackoverflow.com/a/2763014 | <p>Just use <code>alias</code>. Here's a basic example:</p>
<pre><code># Define alias in shell
alias rmi "rm -i"
# Define alias in config file ( `~/.config/fish/config.fish` )
alias rmi="rm -i"
# This is equivalent to entering the following function:
function rmi
rm -i $argv
end
# Then, to sa... | <p>Just use <code>alias</code>. Here's a basic example:</p> <pre><code># Define alias in shell alias rmi "rm -i" # Define alias in config file ( `~/.config/fish/config.fish` ) alias rmi="rm -i" # This is equivalent to entering the following function: function rmi rm -i $argv end # Then, to sa... | 58, 390, 55824 | fish, linux, shell | <h1>Define an alias in fish shell</h1>
<p>I would like to define some aliases in fish. Apparently it should be possible to define them in </p>
<pre><code>~/.config/fish/functions
</code></pre>
<p>but they don't get auto loaded when I restart the shell. Any ideas?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 357 | bash | # Define an alias in fish shell
I would like to define some aliases in fish. Apparently it should be possible to define them in
```
~/.config/fish/functions
```
but they don't get auto loaded when I restart the shell. Any ideas? | Just use `alias`. Here's a basic example:
```
# Define alias in shell
alias rmi "rm -i"
# Define alias in config file ( `~/.config/fish/config.fish` )
alias rmi="rm -i"
# This is equivalent to entering the following function:
function rmi
rm -i $argv
end
# Then, to save it across terminal sessions:
funcsave rmi... |
10856129 | Setting an environment variable before a command in Bash is not working for the second command in a pipe | 614 | 2012-06-01 19:17:20 | <p>In a given shell, normally I'd set a variable or variables and then run a command. Recently I learned about the concept of prepending a variable definition to a command:</p>
<pre><code>FOO=bar somecommand someargs
</code></pre>
<p>This works... kind of. It doesn't work when you're changing a <code>LC_*</code> variab... | 565,454 | 760,905 | 2025-03-21 20:27:45 | 10,856,348 | 528 | 2012-06-01 19:39:08 | 26,428 | 2016-03-25 20:12:54 | https://stackoverflow.com/q/10856129 | https://stackoverflow.com/a/10856348 | <pre><code>FOO=bar bash -c 'somecommand someargs | somecommand2'
</code></pre>
| <pre><code>FOO=bar bash -c 'somecommand someargs | somecommand2' </code></pre> | 387, 9013 | bash, environment-variables | <h1>Setting an environment variable before a command in Bash is not working for the second command in a pipe</h1>
<p>In a given shell, normally I'd set a variable or variables and then run a command. Recently I learned about the concept of prepending a variable definition to a command:</p>
<pre><code>FOO=bar somecomman... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 358 | bash | # Setting an environment variable before a command in Bash is not working for the second command in a pipe
In a given shell, normally I'd set a variable or variables and then run a command. Recently I learned about the concept of prepending a variable definition to a command:
```
FOO=bar somecommand someargs
```
Thi... | ```
FOO=bar bash -c 'somecommand someargs | somecommand2'
``` |
756756 | Multiple commands in an alias for bash | 297 | 2009-04-16 15:47:33 | <p>I'd like to define an alias that runs the following two commands consecutively. </p>
<pre><code>gnome-screensaver
gnome-screensaver-command --lock
</code></pre>
<p>Right now I've added</p>
<pre><code>alias lock='gnome-screensaver-command --lock'
</code></pre>
<p>to my .bashrc but since I lock my workstation so o... | 260,915 | 75,654 | 2021-05-25 04:48:23 | 756,772 | 528 | 2009-04-16 15:51:10 | 45,249 | 2009-04-16 15:51:10 | https://stackoverflow.com/q/756756 | https://stackoverflow.com/a/756772 | <p>Try:</p>
<pre><code>alias lock='gnome-screensaver; gnome-screensaver-command --lock'
</code></pre>
<p>or</p>
<pre><code>lock() {
gnome-screensaver
gnome-screensaver-command --lock
}
</code></pre>
<p>in your .bashrc</p>
<p>The second solution allows you to use arguments.</p>
| <p>Try:</p> <pre><code>alias lock='gnome-screensaver; gnome-screensaver-command --lock' </code></pre> <p>or</p> <pre><code>lock() { gnome-screensaver gnome-screensaver-command --lock } </code></pre> <p>in your .bashrc</p> <p>The second solution allows you to use arguments.</p> | 387, 725 | bash, configuration | <h1>Multiple commands in an alias for bash</h1>
<p>I'd like to define an alias that runs the following two commands consecutively. </p>
<pre><code>gnome-screensaver
gnome-screensaver-command --lock
</code></pre>
<p>Right now I've added</p>
<pre><code>alias lock='gnome-screensaver-command --lock'
</code></pre>
<p>to... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 359 | bash | # Multiple commands in an alias for bash
I'd like to define an alias that runs the following two commands consecutively.
```
gnome-screensaver
gnome-screensaver-command --lock
```
Right now I've added
```
alias lock='gnome-screensaver-command --lock'
```
to my .bashrc but since I lock my workstation so often it wo... | Try:
```
alias lock='gnome-screensaver; gnome-screensaver-command --lock'
```
or
```
lock() {
gnome-screensaver
gnome-screensaver-command --lock
}
```
in your .bashrc
The second solution allows you to use arguments. |
9533679 | How to insert a text at the beginning of a file? | 346 | 2012-03-02 12:55:47 | <p>So far I've been able to find out how to add a line at the beginning of a file but that's not exactly what I want. I'll show it with an example:</p>
<p><strong>File content</strong></p>
<pre><code>some text at the beginning
</code></pre>
<p><strong>Result</strong></p>
<pre><code><added text> some text at the b... | 506,568 | 419,516 | 2023-10-11 20:39:12 | 9,533,736 | 527 | 2012-03-02 13:00:31 | 348,785 | 2017-10-30 18:48:19 | https://stackoverflow.com/q/9533679 | https://stackoverflow.com/a/9533736 | <p><code>sed</code> can operate on an address:</p>
<pre><code>$ sed -i '1s/^/<added text> /' file
</code></pre>
<p>What is this magical <code>1s</code> you see on every answer here? <a href="https://www.gnu.org/software/sed/manual/html_node/Addresses.html" rel="noreferrer">Line addressing!</a>.</p>
<p>Want to ... | <p><code>sed</code> can operate on an address:</p> <pre><code>$ sed -i '1s/^/<added text> /' file </code></pre> <p>What is this magical <code>1s</code> you see on every answer here? <a href="https://www.gnu.org/software/sed/manual/html_node/Addresses.html" rel="noreferrer">Line addressing!</a>.</p> <p>Want to ... | 58, 387, 5282 | bash, linux, sed | <h1>How to insert a text at the beginning of a file?</h1>
<p>So far I've been able to find out how to add a line at the beginning of a file but that's not exactly what I want. I'll show it with an example:</p>
<p><strong>File content</strong></p>
<pre><code>some text at the beginning
</code></pre>
<p><strong>Result</st... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 360 | bash | # How to insert a text at the beginning of a file?
So far I've been able to find out how to add a line at the beginning of a file but that's not exactly what I want. I'll show it with an example:
**File content**
```
some text at the beginning
```
**Result**
```
<added text> some text at the beginning
```
It's si... | `sed` can operate on an address:
```
$ sed -i '1s/^/<added text> /' file
```
What is this magical `1s` you see on every answer here? [Line addressing!](https://www.gnu.org/software/sed/manual/html_node/Addresses.html).
Want to add `<added text>` on the first 10 lines?
```
$ sed -i '1,10s/^/<added text> /' file
```
... |
971879 | What is a unix command for deleting the first N characters of a line? | 364 | 2009-06-09 19:01:35 | <p>For example, I might want to:</p>
<pre><code>tail -f logfile | grep org.springframework | <command to remove first N characters>
</code></pre>
<p>I was thinking that <code>tr</code> might have the ability to do this but I'm not sure.</p>
| 466,704 | 39,489 | 2025-01-09 05:37:39 | 971,906 | 526 | 2009-06-09 19:06:44 | 43,367 | 2025-01-09 05:37:39 | https://stackoverflow.com/q/971879 | https://stackoverflow.com/a/971906 | <p>Use <code>cut</code>. Eg. to strip the first 4 characters of each line (i.e. start on the 5th char):</p>
<pre><code>tail -f logfile | grep --line-buffered org.springframework | cut -c 5-
</code></pre>
| <p>Use <code>cut</code>. Eg. to strip the first 4 characters of each line (i.e. start on the 5th char):</p> <pre><code>tail -f logfile | grep --line-buffered org.springframework | cut -c 5- </code></pre> | 34, 387, 1796, 3601 | bash, command, truncate, unix | <h1>What is a unix command for deleting the first N characters of a line?</h1>
<p>For example, I might want to:</p>
<pre><code>tail -f logfile | grep org.springframework | <command to remove first N characters>
</code></pre>
<p>I was thinking that <code>tr</code> might have the ability to do this but I'm not su... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 361 | bash | # What is a unix command for deleting the first N characters of a line?
For example, I might want to:
```
tail -f logfile | grep org.springframework | <command to remove first N characters>
```
I was thinking that `tr` might have the ability to do this but I'm not sure. | Use `cut`. Eg. to strip the first 4 characters of each line (i.e. start on the 5th char):
```
tail -f logfile | grep --line-buffered org.springframework | cut -c 5-
``` |
13408493 | An "and" operator for an "if" statement in Bash | 393 | 2012-11-16 00:14:11 | <p>I'm trying to create a simple Bash script to check if the website is down and for some reason the "and" operator doesn't work:</p>
<pre><code>#!/usr/bin/env bash
WEBSITE=domain.example
SUBJECT="$WEBSITE DOWN!"
EMAILID="an@email.example"
STATUS=$(curl -sI $WEBSITE | awk '/HTTP\/1.1/ { p... | 996,263 | 1,416,672 | 2026-01-14 00:57:01 | 13,408,590 | 524 | 2012-11-16 00:22:52 | 140,750 | 2023-09-30 23:49:24 | https://stackoverflow.com/q/13408493 | https://stackoverflow.com/a/13408590 | <p>If <code>$STATUS</code> expands to the empty string, then
<code>if [ $STATUS -ne 200 ]</code> is invalid because it is equivalent to
<code>if [ -ne 200 ]</code>, which should generate an error similar to "-ne: unary operator expected".</p>
<p>The command</p>
<pre><code>if [ $STATUS -ne 200 ] -a [[ "$S... | <p>If <code>$STATUS</code> expands to the empty string, then <code>if [ $STATUS -ne 200 ]</code> is invalid because it is equivalent to <code>if [ -ne 200 ]</code>, which should generate an error similar to "-ne: unary operator expected".</p> <p>The command</p> <pre><code>if [ $STATUS -ne 200 ] -a [[ "$S... | 387, 2773 | bash, if-statement | <h1>An "and" operator for an "if" statement in Bash</h1>
<p>I'm trying to create a simple Bash script to check if the website is down and for some reason the "and" operator doesn't work:</p>
<pre><code>#!/usr/bin/env bash
WEBSITE=domain.example
SUBJECT="$WEBSITE DOWN!"
EMAILID="an@email.exampl... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 362 | bash | # An "and" operator for an "if" statement in Bash
I'm trying to create a simple Bash script to check if the website is down and for some reason the "and" operator doesn't work:
```
#!/usr/bin/env bash
WEBSITE=domain.example
SUBJECT="$WEBSITE DOWN!"
EMAILID="an@email.example"
STATUS=$(curl -sI $WEBSITE | awk '/HTTP\/... | If `$STATUS` expands to the empty string, then
`if [ $STATUS -ne 200 ]` is invalid because it is equivalent to
`if [ -ne 200 ]`, which should generate an error similar to "-ne: unary operator expected".
The command
```
if [ $STATUS -ne 200 ] -a [[ "$STRING" != "$VALUE" ]];
```
is invalid regardless of the expansion ... |
5456120 | How to only get file name with Linux 'find'? | 389 | 2011-03-28 07:58:56 | <p>I'm using find to all files in directory, so I get a list of paths. However, I need only file names. i.e. I get <code>./dir1/dir2/file.txt</code> and I want to get <code>file.txt</code></p>
| 435,907 | 171,991 | 2020-05-22 12:43:34 | 5,456,136 | 523 | 2011-03-28 08:00:48 | 207,248 | 2015-10-01 21:14:41 | https://stackoverflow.com/q/5456120 | https://stackoverflow.com/a/5456136 | <p>In GNU <code>find</code> you can use <code>-printf</code> parameter for that, e.g.:</p>
<pre><code>find /dir1 -type f -printf "%f\n"
</code></pre>
| <p>In GNU <code>find</code> you can use <code>-printf</code> parameter for that, e.g.:</p> <pre><code>find /dir1 -type f -printf "%f\n" </code></pre> | 58, 390, 10193 | find, linux, shell | <h1>How to only get file name with Linux 'find'?</h1>
<p>I'm using find to all files in directory, so I get a list of paths. However, I need only file names. i.e. I get <code>./dir1/dir2/file.txt</code> and I want to get <code>file.txt</code></p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 363 | bash | # How to only get file name with Linux 'find'?
I'm using find to all files in directory, so I get a list of paths. However, I need only file names. i.e. I get `./dir1/dir2/file.txt` and I want to get `file.txt` | In GNU `find` you can use `-printf` parameter for that, e.g.:
```
find /dir1 -type f -printf "%f\n"
``` |
30461201 | How do I edit $PATH (.bash_profile) on OS X? | 294 | 2015-05-26 14:17:58 | <p>I am trying to edit an entry to PATH, as I did something wrong.</p>
<p>I am using <a href="https://en.wikipedia.org/wiki/OS_X_Yosemite" rel="noreferrer">Mac OS X v10.10.3</a> (Yosemite)</p>
<p>I have tried:</p>
<pre><code>touch ~/.bash_profile; open ~/.bash_profile
</code></pre>
<p>But the file editor opens with not... | 1,200,321 | 3,792,198 | 2024-07-20 09:20:01 | 30,462,883 | 520 | 2015-05-26 15:31:52 | 799,073 | 2021-04-23 21:58:27 | https://stackoverflow.com/q/30461201 | https://stackoverflow.com/a/30462883 | <p>You have to open that file with a text editor and then save it.</p>
<pre><code>touch ~/.bash_profile; open ~/.bash_profile
</code></pre>
<p>It will open the file with <a href="https://en.wikipedia.org/wiki/TextEdit" rel="noreferrer">TextEdit</a>, paste your things and then save it. If you open it again you'll find y... | <p>You have to open that file with a text editor and then save it.</p> <pre><code>touch ~/.bash_profile; open ~/.bash_profile </code></pre> <p>It will open the file with <a href="https://en.wikipedia.org/wiki/TextEdit" rel="noreferrer">TextEdit</a>, paste your things and then save it. If you open it again you'll find y... | 369, 387, 391, 6268, 104811 | bash, macos, osx-yosemite, path, terminal | <h1>How do I edit $PATH (.bash_profile) on OS X?</h1>
<p>I am trying to edit an entry to PATH, as I did something wrong.</p>
<p>I am using <a href="https://en.wikipedia.org/wiki/OS_X_Yosemite" rel="noreferrer">Mac OS X v10.10.3</a> (Yosemite)</p>
<p>I have tried:</p>
<pre><code>touch ~/.bash_profile; open ~/.bash_profi... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 364 | bash | # How do I edit $PATH (.bash_profile) on OS X?
I am trying to edit an entry to PATH, as I did something wrong.
I am using [Mac OS X v10.10.3](https://en.wikipedia.org/wiki/OS_X_Yosemite) (Yosemite)
I have tried:
```
touch ~/.bash_profile; open ~/.bash_profile
```
But the file editor opens with nothing inside.
My ... | You have to open that file with a text editor and then save it.
```
touch ~/.bash_profile; open ~/.bash_profile
```
It will open the file with [TextEdit](https://en.wikipedia.org/wiki/TextEdit), paste your things and then save it. If you open it again you'll find your edits.
You can use other editors:
```
nano ~/.b... |
18847145 | Loop through files in a directory using PowerShell | 360 | 2013-09-17 10:16:37 | <p>How can I change the following code to look at all the .log files in the directory and not just the one file?</p>
<p>I need to loop through all the files and delete all lines that do not contain "step4" or "step9". Currently this will create a new file, but I'm not sure how to use the <code>for each</code> loop her... | 840,882 | 2,725,402 | 2019-02-13 20:24:41 | 18,848,848 | 518 | 2013-09-17 11:37:13 | 9,833 | 2016-03-22 13:59:55 | https://stackoverflow.com/q/18847145 | https://stackoverflow.com/a/18848848 | <p>Give this a try:</p>
<pre><code>Get-ChildItem "C:\Users\gerhardl\Documents\My Received Files" -Filter *.log |
Foreach-Object {
$content = Get-Content $_.FullName
#filter and save content to the original file
$content | Where-Object {$_ -match 'step[49]'} | Set-Content $_.FullName
#filter and save... | <p>Give this a try:</p> <pre><code>Get-ChildItem "C:\Users\gerhardl\Documents\My Received Files" -Filter *.log | Foreach-Object { $content = Get-Content $_.FullName #filter and save content to the original file $content | Where-Object {$_ -match 'step[49]'} | Set-Content $_.FullName #filter and save... | 526 | powershell | <h1>Loop through files in a directory using PowerShell</h1>
<p>How can I change the following code to look at all the .log files in the directory and not just the one file?</p>
<p>I need to loop through all the files and delete all lines that do not contain "step4" or "step9". Currently this will create a new file, bu... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 365 | bash | # Loop through files in a directory using PowerShell
How can I change the following code to look at all the .log files in the directory and not just the one file?
I need to loop through all the files and delete all lines that do not contain "step4" or "step9". Currently this will create a new file, but I'm not sure h... | Give this a try:
```
Get-ChildItem "C:\Users\gerhardl\Documents\My Received Files" -Filter *.log |
Foreach-Object {
$content = Get-Content $_.FullName
#filter and save content to the original file
$content | Where-Object {$_ -match 'step[49]'} | Set-Content $_.FullName
#filter and save content to a ... |
12382499 | Looking for ALT+LeftArrowKey solution in zsh | 288 | 2012-09-12 06:41:08 | <p>I just recently switched from bash to zsh, however I miss my <kbd>Alt</kbd>+<kbd>LeftArrowKey</kbd> and <kbd>Alt</kbd>+<kbd>RightArrowKey</kbd> to go back and forth a word at a time.</p>
<p>Right now, if I press <kbd>Alt</kbd>+<kbd>LeftArrowKey</kbd> I go back a couple of letters and then I'm stuck. I won't go any ... | 112,417 | 792,750 | 2024-11-01 22:39:29 | 12,403,798 | 518 | 2012-09-13 10:01:26 | 787,216 | 2018-03-03 10:48:26 | https://stackoverflow.com/q/12382499 | https://stackoverflow.com/a/12403798 | <p>Run <code>cat</code> then press keys to see the codes your shortcut send.<br>
(Press <kbd>Ctrl</kbd>+<kbd>C</kbd> to kill the <code>cat</code> when you're done.)<br>
For me, (ubuntu, konsole, xterm) pressing <kbd>Alt</kbd>+<kbd>←</kbd> sends <code>^[[1;3D</code>, so i would put in my <em>.zshrc</em></p>
<pre><code>... | <p>Run <code>cat</code> then press keys to see the codes your shortcut send.<br> (Press <kbd>Ctrl</kbd>+<kbd>C</kbd> to kill the <code>cat</code> when you're done.)<br> For me, (ubuntu, konsole, xterm) pressing <kbd>Alt</kbd>+<kbd>←</kbd> sends <code>^[[1;3D</code>, so i would put in my <em>.zshrc</em></p> <pre><code>... | 390, 3791, 5350 | key, shell, zsh | <h1>Looking for ALT+LeftArrowKey solution in zsh</h1>
<p>I just recently switched from bash to zsh, however I miss my <kbd>Alt</kbd>+<kbd>LeftArrowKey</kbd> and <kbd>Alt</kbd>+<kbd>RightArrowKey</kbd> to go back and forth a word at a time.</p>
<p>Right now, if I press <kbd>Alt</kbd>+<kbd>LeftArrowKey</kbd> I go back a... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 366 | bash | # Looking for ALT+LeftArrowKey solution in zsh
I just recently switched from bash to zsh, however I miss my `Alt`+`LeftArrowKey` and `Alt`+`RightArrowKey` to go back and forth a word at a time.
Right now, if I press `Alt`+`LeftArrowKey` I go back a couple of letters and then I'm stuck. I won't go any further backward... | Run `cat` then press keys to see the codes your shortcut send.
(Press `Ctrl`+`C` to kill the `cat` when you're done.)
For me, (ubuntu, konsole, xterm) pressing `Alt`+`←` sends `^[[1;3D`, so i would put in my *.zshrc*
```
bindkey "^[[1;3C" forward-word
bindkey "^[[1;3D" backward-word
```
(Actually I prefer to use ... |
22465332 | Setting PATH environment variable in macOS permanently | 301 | 2014-03-17 21:02:04 | <p>I have read several answers on how to set environment variables on macOS permanently.</p>
<p>First, I tried this, <a href="https://stackoverflow.com/questions/14637979/how-to-permanently-set-path-on-linux">How to permanently set $PATH on Linux/Unix</a> but I had an error message saying <code>no such file and directo... | 723,935 | 3,399,468 | 2025-04-28 10:07:12 | 22,465,399 | 517 | 2014-03-17 21:06:44 | 3,002,709 | 2024-11-21 21:23:19 | https://stackoverflow.com/q/22465332 | https://stackoverflow.com/a/22465399 | <p>You have to add it to <code>/etc/paths</code>.</p>
<p>Reference (which works for me) : <a href="http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.Uydjga1dXDg" rel="noreferrer">Here</a></p>
<hr />
<p>These are the steps from the referenced article:</p>
<ul>
<li>Open up Terminal.</li>
<li... | <p>You have to add it to <code>/etc/paths</code>.</p> <p>Reference (which works for me) : <a href="http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.Uydjga1dXDg" rel="noreferrer">Here</a></p> <hr /> <p>These are the steps from the referenced article:</p> <ul> <li>Open up Terminal.</li> <li... | 34, 369, 387, 6268, 9013 | bash, environment-variables, macos, path, unix | <h1>Setting PATH environment variable in macOS permanently</h1>
<p>I have read several answers on how to set environment variables on macOS permanently.</p>
<p>First, I tried this, <a href="https://stackoverflow.com/questions/14637979/how-to-permanently-set-path-on-linux">How to permanently set $PATH on Linux/Unix</a> ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 367 | bash | # Setting PATH environment variable in macOS permanently
I have read several answers on how to set environment variables on macOS permanently.
First, I tried this, [How to permanently set $PATH on Linux/Unix](https://stackoverflow.com/questions/14637979/how-to-permanently-set-path-on-linux) but I had an error message... | You have to add it to `/etc/paths`.
Reference (which works for me) : [Here](http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.Uydjga1dXDg)
---
These are the steps from the referenced article:
- Open up Terminal.
- Run the following command: `sudo nano /etc/paths`
- Enter your password,... |
63805 | Equivalent of *Nix 'which' command in PowerShell? | 521 | 2008-09-15 15:13:59 | <p>How do I ask PowerShell where something is? </p>
<p>For instance, "which notepad" and it returns the directory where the notepad.exe is run from according to the current paths.</p>
| 157,216 | 1,220 | 2024-02-07 03:20:46 | 65,148 | 514 | 2008-09-15 17:56:32 | 6,637 | 2018-12-09 07:49:55 | https://stackoverflow.com/q/63805 | https://stackoverflow.com/a/65148 | <p>The very first alias I made once I started customizing my profile in PowerShell was 'which'.</p>
<pre class="lang-sh prettyprint-override"><code>New-Alias which get-command
</code></pre>
<p>To add this to your profile, type this:</p>
<pre class="lang-sh prettyprint-override"><code>"`nNew-Alias which get-command" ... | <p>The very first alias I made once I started customizing my profile in PowerShell was 'which'.</p> <pre class="lang-sh prettyprint-override"><code>New-Alias which get-command </code></pre> <p>To add this to your profile, type this:</p> <pre class="lang-sh prettyprint-override"><code>"`nNew-Alias which get-command" ... | 34, 526, 1796 | command, powershell, unix | <h1>Equivalent of *Nix 'which' command in PowerShell?</h1>
<p>How do I ask PowerShell where something is? </p>
<p>For instance, "which notepad" and it returns the directory where the notepad.exe is run from according to the current paths.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 368 | bash | # Equivalent of *Nix 'which' command in PowerShell?
How do I ask PowerShell where something is?
For instance, "which notepad" and it returns the directory where the notepad.exe is run from according to the current paths. | The very first alias I made once I started customizing my profile in PowerShell was 'which'.
```
New-Alias which get-command
```
To add this to your profile, type this:
```
"`nNew-Alias which get-command" | add-content $profile
```
The `n at the start of the last line is to ensure it will start as a new line. |
13868821 | Shell script to delete directories older than n days | 222 | 2012-12-13 21:30:19 | <p>I have directories named as:</p>
<pre><code>2012-12-12
2012-10-12
2012-08-08
</code></pre>
<p>How would I delete the directories that are older than 10 days with a bash shell script?</p>
| 349,463 | 347,039 | 2020-06-02 16:16:04 | 13,869,000 | 514 | 2012-12-13 21:44:01 | 1,741,864 | 2018-07-18 19:51:56 | https://stackoverflow.com/q/13868821 | https://stackoverflow.com/a/13869000 | <p>This will do it recursively for you:</p>
<pre><code>find /path/to/base/dir/* -type d -ctime +10 -exec rm -rf {} \;
</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>find</code>: the unix command for finding files / directories / links etc.</li>
<li><code>/path/to/base/dir</code>: the directory to ... | <p>This will do it recursively for you:</p> <pre><code>find /path/to/base/dir/* -type d -ctime +10 -exec rm -rf {} \; </code></pre> <p><strong>Explanation:</strong></p> <ul> <li><code>find</code>: the unix command for finding files / directories / links etc.</li> <li><code>/path/to/base/dir</code>: the directory to ... | 387, 390 | bash, shell | <h1>Shell script to delete directories older than n days</h1>
<p>I have directories named as:</p>
<pre><code>2012-12-12
2012-10-12
2012-08-08
</code></pre>
<p>How would I delete the directories that are older than 10 days with a bash shell script?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 369 | bash | # Shell script to delete directories older than n days
I have directories named as:
```
2012-12-12
2012-10-12
2012-08-08
```
How would I delete the directories that are older than 10 days with a bash shell script? | This will do it recursively for you:
```
find /path/to/base/dir/* -type d -ctime +10 -exec rm -rf {} \;
```
**Explanation:**
- `find`: the unix command for finding files / directories / links etc.
- `/path/to/base/dir`: the directory to start your search in.
- `-type d`: only find directories
- `-ctime +10`: only co... |
6495501 | "find: paths must precede expression:" How do I specify a recursive search that also finds files in the current directory? | 311 | 2011-06-27 15:51:34 | <p>I am having a hard time getting <strong>find</strong> to look for matches in the current directory as well as its subdirectories. </p>
<p>When I run <code>find *test.c</code> it only gives me the matches in the current directory. (does not look in subdirectories)</p>
<p>If I try <code>find . -name *test.c</code> I... | 382,618 | 425,683 | 2025-08-28 22:29:45 | 6,495,536 | 508 | 2011-06-27 15:54:56 | 130,352 | 2011-06-27 15:54:56 | https://stackoverflow.com/q/6495501 | https://stackoverflow.com/a/6495536 | <p>Try putting it in quotes -- you're running into the shell's wildcard expansion, so what you're acually passing to find will look like:</p>
<pre><code>find . -name bobtest.c cattest.c snowtest.c
</code></pre>
<p>...causing the syntax error. So try this instead:</p>
<pre><code>find . -name '*test.c'
</code></pre>
... | <p>Try putting it in quotes -- you're running into the shell's wildcard expansion, so what you're acually passing to find will look like:</p> <pre><code>find . -name bobtest.c cattest.c snowtest.c </code></pre> <p>...causing the syntax error. So try this instead:</p> <pre><code>find . -name '*test.c' </code></pre> ... | 387, 10193 | bash, find | <h1>"find: paths must precede expression:" How do I specify a recursive search that also finds files in the current directory?</h1>
<p>I am having a hard time getting <strong>find</strong> to look for matches in the current directory as well as its subdirectories. </p>
<p>When I run <code>find *test.c</code> it only g... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 370 | bash | # "find: paths must precede expression:" How do I specify a recursive search that also finds files in the current directory?
I am having a hard time getting **find** to look for matches in the current directory as well as its subdirectories.
When I run `find *test.c` it only gives me the matches in the current direct... | Try putting it in quotes -- you're running into the shell's wildcard expansion, so what you're acually passing to find will look like:
```
find . -name bobtest.c cattest.c snowtest.c
```
...causing the syntax error. So try this instead:
```
find . -name '*test.c'
```
Note the single quotes around your file expressi... |
12967232 | Repeatedly run a shell command until it fails? | 301 | 2012-10-19 04:05:02 | <p>I've written a <a href="http://en.wikipedia.org/wiki/Fuzz_testing">fuzzy</a> test that fails unreliably. I've added some debug code, but now I want to run the test until it fails so I can gather the debug output.</p>
<p>I've setup the test so I can run it using:</p>
<pre><code>./runtest
</code></pre>
<p>My curren... | 102,934 | 253,686 | 2022-10-01 18:36:36 | 12,967,264 | 507 | 2012-10-19 04:09:14 | 1,204,143 | 2019-11-08 00:10:56 | https://stackoverflow.com/q/12967232 | https://stackoverflow.com/a/12967264 | <p><code>while</code> takes a command to execute, so you can use the simpler</p>
<pre><code>while ./runtest; do :; done
</code></pre>
<p>This will stop the loop when <code>./runtest</code> returns a <em>nonzero</em> exit code (which is usually indicative of failure).</p>
<p>To further simplify your current solution ... | <p><code>while</code> takes a command to execute, so you can use the simpler</p> <pre><code>while ./runtest; do :; done </code></pre> <p>This will stop the loop when <code>./runtest</code> returns a <em>nonzero</em> exit code (which is usually indicative of failure).</p> <p>To further simplify your current solution ... | 387 | bash | <h1>Repeatedly run a shell command until it fails?</h1>
<p>I've written a <a href="http://en.wikipedia.org/wiki/Fuzz_testing">fuzzy</a> test that fails unreliably. I've added some debug code, but now I want to run the test until it fails so I can gather the debug output.</p>
<p>I've setup the test so I can run it usin... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 371 | bash | # Repeatedly run a shell command until it fails?
I've written a [fuzzy](http://en.wikipedia.org/wiki/Fuzz_testing) test that fails unreliably. I've added some debug code, but now I want to run the test until it fails so I can gather the debug output.
I've setup the test so I can run it using:
```
./runtest
```
My c... | `while` takes a command to execute, so you can use the simpler
```
while ./runtest; do :; done
```
This will stop the loop when `./runtest` returns a *nonzero* exit code (which is usually indicative of failure).
To further simplify your current solution though, you should just change your untilfail script to look li... |
6723426 | Looping over arrays, printing both index and value | 296 | 2011-07-17 11:02:13 | <p>I want to do something like this:</p>
<pre><code>foo=( )
foo[0]="bar"
foo[35]="baz"
for((i=0;i<${#foo[@]};i++))
do
echo "$i: ${foo[$i]}"
done
# Output:
# 0: bar
# 1:
</code></pre>
<p>Then i tried to loop through it using for in:</p>
<pre><code>foo=( )
foo[0]="bar"
foo[35]="baz"
for i in ${foo[@]}
do
e... | 343,472 | 640,584 | 2025-07-03 19:15:31 | 6,723,516 | 507 | 2011-07-17 11:26:53 | 7,552 | 2019-02-22 20:38:39 | https://stackoverflow.com/q/6723426 | https://stackoverflow.com/a/6723516 | <p>You would find the array keys with <code>"${!foo[@]}"</code> (<a href="http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion" rel="noreferrer">reference</a>), so:</p>
<pre><code>for i in "${!foo[@]}"; do
printf "%s\t%s\n" "$i" "${foo[$i]}"
done
</code></pre>
<p>Which means that indices ... | <p>You would find the array keys with <code>"${!foo[@]}"</code> (<a href="http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion" rel="noreferrer">reference</a>), so:</p> <pre><code>for i in "${!foo[@]}"; do printf "%s\t%s\n" "$i" "${foo[$i]}" done </code></pre> <p>Which means that indices ... | 387 | bash | <h1>Looping over arrays, printing both index and value</h1>
<p>I want to do something like this:</p>
<pre><code>foo=( )
foo[0]="bar"
foo[35]="baz"
for((i=0;i<${#foo[@]};i++))
do
echo "$i: ${foo[$i]}"
done
# Output:
# 0: bar
# 1:
</code></pre>
<p>Then i tried to loop through it using for in:</p>
<pre><code>fo... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 372 | bash | # Looping over arrays, printing both index and value
I want to do something like this:
```
foo=( )
foo[0]="bar"
foo[35]="baz"
for((i=0;i<${#foo[@]};i++))
do
echo "$i: ${foo[$i]}"
done
# Output:
# 0: bar
# 1:
```
Then i tried to loop through it using for in:
```
foo=( )
foo[0]="bar"
foo[35]="baz"
for i in ${foo[... | You would find the array keys with `"${!foo[@]}"` ([reference](http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion)), so:
```
for i in "${!foo[@]}"; do
printf "%s\t%s\n" "$i" "${foo[$i]}"
done
```
Which means that indices will be in `$i` while the elements themselves have to be accessed ... |
2893954 | How to pass in password to pg_dump? | 507 | 2010-05-23 23:32:11 | <p>I'm trying to create a cronjob to back up my database every night before something catastrophic happens. It looks like this command should meet my needs:</p>
<pre><code>0 3 * * * pg_dump dbname | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz
</code></pre>
<p>Except after running that, it expects me to type in a ... | 568,105 | 65,387 | 2025-04-17 21:03:16 | 2,893,979 | 506 | 2010-05-23 23:39:55 | 85,134 | 2020-11-10 10:18:06 | https://stackoverflow.com/q/2893954 | https://stackoverflow.com/a/2893979 | <p>Create a <code>.pgpass</code> file in the home directory of the account that <code>pg_dump</code> will run as.</p>
<p>The format is:</p>
<pre><code>hostname:port:database:username:password
</code></pre>
<p>Then, set the file's mode to <code>0600</code>. Otherwise, it will be ignored.</p>
<pre><code>chmod 600 ~/.pgpa... | <p>Create a <code>.pgpass</code> file in the home directory of the account that <code>pg_dump</code> will run as.</p> <p>The format is:</p> <pre><code>hostname:port:database:username:password </code></pre> <p>Then, set the file's mode to <code>0600</code>. Otherwise, it will be ignored.</p> <pre><code>chmod 600 ~/.pgpa... | 256, 387, 390, 601 | bash, cron, postgresql, shell | <h1>How to pass in password to pg_dump?</h1>
<p>I'm trying to create a cronjob to back up my database every night before something catastrophic happens. It looks like this command should meet my needs:</p>
<pre><code>0 3 * * * pg_dump dbname | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz
</code></pre>
<p>Except af... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 373 | bash | # How to pass in password to pg_dump?
I'm trying to create a cronjob to back up my database every night before something catastrophic happens. It looks like this command should meet my needs:
```
0 3 * * * pg_dump dbname | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz
```
Except after running that, it expects me to t... | Create a `.pgpass` file in the home directory of the account that `pg_dump` will run as.
The format is:
```
hostname:port:database:username:password
```
Then, set the file's mode to `0600`. Otherwise, it will be ignored.
```
chmod 600 ~/.pgpass
```
See the Postgresql documentation [libpq-pgpass](https://www.postgr... |
13158083 | Take a full page screenshot with Firefox on the command-line | 239 | 2012-10-31 12:34:37 | <p>I'm running Firefox on a Xvfb in a VPS. What I want to do is to take a full page screenshot of the page.</p>
<p>I can redirect Firefox to particular page using</p>
<pre><code>firefox http://google.com
</code></pre>
<p>and take a screenshot (inside X) using ImageMagick</p>
<pre><code>import root -window output.jp... | 237,468 | 12,637 | 2021-04-28 08:01:23 | 14,830,242 | 505 | 2013-02-12 10:22:16 | 968,925 | 2021-04-28 08:01:23 | https://stackoverflow.com/q/13158083 | https://stackoverflow.com/a/14830242 | <p>The <a href="https://mail.mozilla.org/pipermail/firefox-dev/2018-March/006249.html" rel="noreferrer">Developer Toolbar GCLI and <kbd>Shift</kbd>+<kbd>F2</kbd> shortcut were removed in Firefox version 60</a>. To take a screenshot in 60 or newer:</p>
<ul>
<li>press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>K</kbd> to open... | <p>The <a href="https://mail.mozilla.org/pipermail/firefox-dev/2018-March/006249.html" rel="noreferrer">Developer Toolbar GCLI and <kbd>Shift</kbd>+<kbd>F2</kbd> shortcut were removed in Firefox version 60</a>. To take a screenshot in 60 or newer:</p> <ul> <li>press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>K</kbd> to open... | 390, 691, 1231, 2603 | command-line, firefox, screenshot, shell | <h1>Take a full page screenshot with Firefox on the command-line</h1>
<p>I'm running Firefox on a Xvfb in a VPS. What I want to do is to take a full page screenshot of the page.</p>
<p>I can redirect Firefox to particular page using</p>
<pre><code>firefox http://google.com
</code></pre>
<p>and take a screenshot (ins... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 374 | bash | # Take a full page screenshot with Firefox on the command-line
I'm running Firefox on a Xvfb in a VPS. What I want to do is to take a full page screenshot of the page.
I can redirect Firefox to particular page using
```
firefox http://google.com
```
and take a screenshot (inside X) using ImageMagick
```
import roo... | The [Developer Toolbar GCLI and `Shift`+`F2` shortcut were removed in Firefox version 60](https://mail.mozilla.org/pipermail/firefox-dev/2018-March/006249.html). To take a screenshot in 60 or newer:
- press `Ctrl`+`Shift`+`K` to open the developer console (`⌥ Option`+`⌘ Command`+`K` on macOS)
- type `:screenshot` or `... |
3466166 | How to check if running in Cygwin, Mac or Linux? | 490 | 2010-08-12 09:10:22 | <p>I have a shell script that is used both on Windows/Cygwin and Mac and Linux. It needs slightly different variables for each versions.</p>
<p>How can a shell/bash script detect whether it is running in Cygwin, on a Mac or in Linux?</p>
| 250,745 | 1,034 | 2024-10-24 22:08:38 | 3,466,183 | 504 | 2010-08-12 09:12:14 | 14,860 | 2024-10-24 22:08:38 | https://stackoverflow.com/q/3466166 | https://stackoverflow.com/a/3466183 | <p>Usually, <code>uname</code> with its various options will tell you what environment you're running in:</p>
<pre class="lang-bash prettyprint-override"><code>pax> uname -a
CYGWIN_NT-5.1 IBM-L3F3936 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin
pax> uname -s
CYGWIN_NT-5.1
</code></pre>
<p>And, according to the... | <p>Usually, <code>uname</code> with its various options will tell you what environment you're running in:</p> <pre class="lang-bash prettyprint-override"><code>pax> uname -a CYGWIN_NT-5.1 IBM-L3F3936 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin pax> uname -s CYGWIN_NT-5.1 </code></pre> <p>And, according to the... | 387, 390, 1731, 3285 | bash, cross-platform, cygwin, shell | <h1>How to check if running in Cygwin, Mac or Linux?</h1>
<p>I have a shell script that is used both on Windows/Cygwin and Mac and Linux. It needs slightly different variables for each versions.</p>
<p>How can a shell/bash script detect whether it is running in Cygwin, on a Mac or in Linux?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 375 | bash | # How to check if running in Cygwin, Mac or Linux?
I have a shell script that is used both on Windows/Cygwin and Mac and Linux. It needs slightly different variables for each versions.
How can a shell/bash script detect whether it is running in Cygwin, on a Mac or in Linux? | Usually, `uname` with its various options will tell you what environment you're running in:
```
pax> uname -a
CYGWIN_NT-5.1 IBM-L3F3936 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin
pax> uname -s
CYGWIN_NT-5.1
```
And, according to the very helpful `schot` (in the comments), `uname -s` gives `Darwin` for OSX and `L... |
90418 | Exit Shell Script Based on Process Exit Code | 386 | 2008-09-18 06:03:48 | <p>I have a shell script that executes a number of commands. How do I make the shell script exit if any of the commands exit with a non-zero exit code?</p>
| 433,417 | 9,940 | 2021-02-19 08:44:51 | 90,435 | 503 | 2008-09-18 06:08:52 | 14,860 | 2016-01-07 01:24:37 | https://stackoverflow.com/q/90418 | https://stackoverflow.com/a/90435 | <p>After each command, the exit code can be found in the <code>$?</code> variable so you would have something like:</p>
<pre><code>ls -al file.ext
rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi
</code></pre>
<p>You need to be careful of piped commands since the <code>$?</code> only gives you the return code of the last ... | <p>After each command, the exit code can be found in the <code>$?</code> variable so you would have something like:</p> <pre><code>ls -al file.ext rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi </code></pre> <p>You need to be careful of piped commands since the <code>$?</code> only gives you the return code of the last ... | 387, 390 | bash, shell | <h1>Exit Shell Script Based on Process Exit Code</h1>
<p>I have a shell script that executes a number of commands. How do I make the shell script exit if any of the commands exit with a non-zero exit code?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 376 | bash | # Exit Shell Script Based on Process Exit Code
I have a shell script that executes a number of commands. How do I make the shell script exit if any of the commands exit with a non-zero exit code? | After each command, the exit code can be found in the `$?` variable so you would have something like:
```
ls -al file.ext
rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi
```
You need to be careful of piped commands since the `$?` only gives you the return code of the last element in the pipe so, in the code:
```
ls -al ... |
42494853 | standard_init_linux.go:178: exec user process caused "exec format error" | 353 | 2017-02-27 20:08:54 | <p>docker started throwing this error:</p>
<blockquote>
<p>standard_init_linux.go:178: exec user process caused "exec format error"</p>
</blockquote>
<p>whenever I run a specific docker container with CMD or ENTRYPOINT, with no regard to any changes to the file other then removing CMD or ENTRYPOINT. here is the doc... | 421,731 | 3,901,400 | 2024-05-06 07:35:28 | 42,494,944 | 503 | 2017-02-27 20:14:52 | 3,901,400 | 2017-02-27 20:14:52 | https://stackoverflow.com/q/42494853 | https://stackoverflow.com/a/42494944 | <p>I forgot to put </p>
<pre><code>#!/bin/bash
</code></pre>
<p>at the top of the sh file, problem solved.</p>
| <p>I forgot to put </p> <pre><code>#!/bin/bash </code></pre> <p>at the top of the sh file, problem solved.</p> | 16, 58, 387, 90304 | bash, docker, linux, python | <h1>standard_init_linux.go:178: exec user process caused "exec format error"</h1>
<p>docker started throwing this error:</p>
<blockquote>
<p>standard_init_linux.go:178: exec user process caused "exec format error"</p>
</blockquote>
<p>whenever I run a specific docker container with CMD or ENTRYPOINT, with no regard... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 377 | bash | # standard_init_linux.go:178: exec user process caused "exec format error"
docker started throwing this error:
> standard_init_linux.go:178: exec user process caused "exec format error"
whenever I run a specific docker container with CMD or ENTRYPOINT, with no regard to any changes to the file other then removing CM... | I forgot to put
```
#!/bin/bash
```
at the top of the sh file, problem solved. |
9948517 | How to stop a PowerShell script on the first error? | 437 | 2012-03-30 18:27:33 | <p>I want my PowerShell script to stop when any of the commands I run fail (like <code>set -e</code> in bash). I'm using both Powershell commands (<code>New-Object System.Net.WebClient</code>) and programs (<code>.\setup.exe</code>).</p>
| 302,264 | 237,285 | 2024-04-13 14:28:38 | 9,949,909 | 502 | 2012-03-30 20:20:07 | 153,982 | 2017-06-22 20:03:50 | https://stackoverflow.com/q/9948517 | https://stackoverflow.com/a/9949909 | <p><code>$ErrorActionPreference = "Stop"</code> will get you part of the way there (i.e. this works great for cmdlets).</p>
<p>However for EXEs you're going to need to check <code>$LastExitCode</code> yourself after every exe invocation and determine whether that failed or not. Unfortunately I don't think PowerShell c... | <p><code>$ErrorActionPreference = "Stop"</code> will get you part of the way there (i.e. this works great for cmdlets).</p> <p>However for EXEs you're going to need to check <code>$LastExitCode</code> yourself after every exe invocation and determine whether that failed or not. Unfortunately I don't think PowerShell c... | 64, 526 | powershell, windows | <h1>How to stop a PowerShell script on the first error?</h1>
<p>I want my PowerShell script to stop when any of the commands I run fail (like <code>set -e</code> in bash). I'm using both Powershell commands (<code>New-Object System.Net.WebClient</code>) and programs (<code>.\setup.exe</code>).</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 378 | bash | # How to stop a PowerShell script on the first error?
I want my PowerShell script to stop when any of the commands I run fail (like `set -e` in bash). I'm using both Powershell commands (`New-Object System.Net.WebClient`) and programs (`.\setup.exe`). | `$ErrorActionPreference = "Stop"` will get you part of the way there (i.e. this works great for cmdlets).
However for EXEs you're going to need to check `$LastExitCode` yourself after every exe invocation and determine whether that failed or not. Unfortunately I don't think PowerShell can help here because on Windows,... |
2437452 | How to get the list of files in a directory in a shell script? | 305 | 2010-03-13 06:03:45 | <p>I'm trying to get the contents of a directory using shell script.</p>
<p>My script is:</p>
<pre><code>for entry in `ls $search_dir`; do
echo $entry
done
</code></pre>
<p>where <code>$search_dir</code> is a relative path. However, <code>$search_dir</code> contains many files with whitespaces in their names. In... | 1,131,742 | 2,119,053 | 2024-07-26 09:06:45 | 2,437,466 | 501 | 2010-03-13 06:08:11 | 20,862 | 2023-01-06 22:38:53 | https://stackoverflow.com/q/2437452 | https://stackoverflow.com/a/2437466 | <pre><code>search_dir=/the/path/to/base/dir
for entry in "$search_dir"/*
do
echo "$entry"
done
</code></pre>
| <pre><code>search_dir=/the/path/to/base/dir for entry in "$search_dir"/* do echo "$entry" done </code></pre> | 387, 390, 9873 | bash, directory-listing, shell | <h1>How to get the list of files in a directory in a shell script?</h1>
<p>I'm trying to get the contents of a directory using shell script.</p>
<p>My script is:</p>
<pre><code>for entry in `ls $search_dir`; do
echo $entry
done
</code></pre>
<p>where <code>$search_dir</code> is a relative path. However, <code>$s... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 379 | bash | # How to get the list of files in a directory in a shell script?
I'm trying to get the contents of a directory using shell script.
My script is:
```
for entry in `ls $search_dir`; do
echo $entry
done
```
where `$search_dir` is a relative path. However, `$search_dir` contains many files with whitespaces in their... | ```
search_dir=/the/path/to/base/dir
for entry in "$search_dir"/*
do
echo "$entry"
done
``` |
10382141 | Temporarily change current working directory in bash to run a command | 281 | 2012-04-30 10:28:21 | <p>I know I can use <code>cd</code> command to change my working directory in bash. </p>
<p>But if I do this command:</p>
<pre><code>cd SOME_PATH && run_some_command
</code></pre>
<p>Then the working directory will be changed permanently. Is there some way to change the working directory just temporarily li... | 217,117 | 619,292 | 2016-06-17 22:40:36 | 10,382,170 | 501 | 2012-04-30 10:30:33 | 227,665 | 2014-04-24 07:38:36 | https://stackoverflow.com/q/10382141 | https://stackoverflow.com/a/10382170 | <p>You can run the <code>cd</code> and the executable in a subshell by enclosing the command line in a pair of parentheses:</p>
<pre><code>(cd SOME_PATH && exec_some_command)
</code></pre>
<p>Demo:</p>
<pre><code>$ pwd
/home/abhijit
$ (cd /tmp && pwd) # directory changed in the subshell
/tmp
$ pwd ... | <p>You can run the <code>cd</code> and the executable in a subshell by enclosing the command line in a pair of parentheses:</p> <pre><code>(cd SOME_PATH && exec_some_command) </code></pre> <p>Demo:</p> <pre><code>$ pwd /home/abhijit $ (cd /tmp && pwd) # directory changed in the subshell /tmp $ pwd ... | 387, 390, 391, 10201 | bash, shell, terminal, working-directory | <h1>Temporarily change current working directory in bash to run a command</h1>
<p>I know I can use <code>cd</code> command to change my working directory in bash. </p>
<p>But if I do this command:</p>
<pre><code>cd SOME_PATH && run_some_command
</code></pre>
<p>Then the working directory will be changed per... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 380 | bash | # Temporarily change current working directory in bash to run a command
I know I can use `cd` command to change my working directory in bash.
But if I do this command:
```
cd SOME_PATH && run_some_command
```
Then the working directory will be changed permanently. Is there some way to change the working directory j... | You can run the `cd` and the executable in a subshell by enclosing the command line in a pair of parentheses:
```
(cd SOME_PATH && exec_some_command)
```
Demo:
```
$ pwd
/home/abhijit
$ (cd /tmp && pwd) # directory changed in the subshell
/tmp
$ pwd # parent shell's pwd is still the same
/home/abhiji... |
9377040 | Remove duplicate entries in a Bash script | 226 | 2012-02-21 11:46:43 | <p>I want to remove duplicate entries from a text file, e.g:</p>
<pre><code>kavitha= Tue Feb 20 14:00 19 IST 2012 (duplicate entry)
sree=Tue Jan 20 14:05 19 IST 2012
divya = Tue Jan 20 14:20 19 IST 2012
anusha=Tue Jan 20 14:45 19 IST 2012
kavitha= Tue Feb 20 14:00 19 IST 2012 (duplicate entry)
</code><... | 243,647 | 827,583 | 2020-09-28 15:04:31 | 9,377,125 | 500 | 2012-02-21 11:52:57 | 348,785 | 2016-04-23 19:36:13 | https://stackoverflow.com/q/9377040 | https://stackoverflow.com/a/9377125 | <p>You can <code>sort</code> then <code>uniq</code>:</p>
<pre><code>$ sort -u input.txt
</code></pre>
<p>Or use <code>awk</code>:</p>
<pre><code>$ awk '!a[$0]++' input.txt
</code></pre>
| <p>You can <code>sort</code> then <code>uniq</code>:</p> <pre><code>$ sort -u input.txt </code></pre> <p>Or use <code>awk</code>:</p> <pre><code>$ awk '!a[$0]++' input.txt </code></pre> | 387, 390 | bash, shell | <h1>Remove duplicate entries in a Bash script</h1>
<p>I want to remove duplicate entries from a text file, e.g:</p>
<pre><code>kavitha= Tue Feb 20 14:00 19 IST 2012 (duplicate entry)
sree=Tue Jan 20 14:05 19 IST 2012
divya = Tue Jan 20 14:20 19 IST 2012
anusha=Tue Jan 20 14:45 19 IST 2012
kavitha= Tue Feb ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 381 | bash | # Remove duplicate entries in a Bash script
I want to remove duplicate entries from a text file, e.g:
```
kavitha= Tue Feb 20 14:00 19 IST 2012 (duplicate entry)
sree=Tue Jan 20 14:05 19 IST 2012
divya = Tue Jan 20 14:20 19 IST 2012
anusha=Tue Jan 20 14:45 19 IST 2012
kavitha= Tue Feb 20 14:00 19 IST 2... | You can `sort` then `uniq`:
```
$ sort -u input.txt
```
Or use `awk`:
```
$ awk '!a[$0]++' input.txt
``` |
4632028 | How to create a temporary directory? | 352 | 2011-01-08 02:28:39 | <p>I used to create a <code>tempfile</code>, delete it and recreate it as a directory:</p>
<pre><code>temp=`tempfile`
rm -f $temp
# <breakpoint>
mkdir $temp
</code></pre>
<p>The problem is, when it runs to the <em><breakpoint></em>, there happens to be another program that wants to do the same thing, whic... | 278,255 | 217,071 | 2024-01-31 20:10:19 | 4,632,032 | 497 | 2011-01-08 02:30:15 | 89,806 | 2011-01-08 02:30:15 | https://stackoverflow.com/q/4632028 | https://stackoverflow.com/a/4632032 | <p>Use <a href="http://www.mktemp.org/" rel="noreferrer"><code>mktemp -d</code></a>. It creates a temporary directory with a random name and makes sure that file doesn't already exist. You need to remember to delete the directory after using it though.</p>
| <p>Use <a href="http://www.mktemp.org/" rel="noreferrer"><code>mktemp -d</code></a>. It creates a temporary directory with a random name and makes sure that file doesn't already exist. You need to remember to delete the directory after using it though.</p> | 387, 25827 | bash, temporary-directory | <h1>How to create a temporary directory?</h1>
<p>I used to create a <code>tempfile</code>, delete it and recreate it as a directory:</p>
<pre><code>temp=`tempfile`
rm -f $temp
# <breakpoint>
mkdir $temp
</code></pre>
<p>The problem is, when it runs to the <em><breakpoint></em>, there happens to be another... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 382 | bash | # How to create a temporary directory?
I used to create a `tempfile`, delete it and recreate it as a directory:
```
temp=`tempfile`
rm -f $temp
# <breakpoint>
mkdir $temp
```
The problem is, when it runs to the *<breakpoint>*, there happens to be another program that wants to do the same thing, which *mkdir*-ed a ... | Use [`mktemp -d`](http://www.mktemp.org/). It creates a temporary directory with a random name and makes sure that file doesn't already exist. You need to remember to delete the directory after using it though. |
16758525 | Make xargs handle filenames that contain spaces | 442 | 2013-05-26 10:52:59 | <pre><code>$ ls *mp3 | xargs mplayer
Playing Lemon.
File not found: 'Lemon'
Playing Tree.mp3.
File not found: 'Tree.mp3'
Exiting... (End of file)
</code></pre>
<p>My command fails because the file "Lemon Tree.mp3" contains spaces and so xargs thinks it's two files. Can I make find + xargs work with filen... | 175,691 | 1,982,032 | 2025-09-17 01:49:28 | 32,589,977 | 495 | 2015-09-15 15:28:45 | 268,284 | 2021-05-30 12:12:51 | https://stackoverflow.com/q/16758525 | https://stackoverflow.com/a/32589977 | <p>The <code>xargs</code> command takes white space characters (tabs, spaces, new lines) as delimiters.</p>
<p>You can narrow it down only for the new line characters ('\n') with <code>-d</code> option like this:</p>
<pre><code>ls *.mp3 | xargs -d '\n' mplayer
</code></pre>
<p>It works only with GNU xargs.</p>
<p>For M... | <p>The <code>xargs</code> command takes white space characters (tabs, spaces, new lines) as delimiters.</p> <p>You can narrow it down only for the new line characters ('\n') with <code>-d</code> option like this:</p> <pre><code>ls *.mp3 | xargs -d '\n' mplayer </code></pre> <p>It works only with GNU xargs.</p> <p>For M... | 390, 10193, 10326 | find, shell, xargs | <h1>Make xargs handle filenames that contain spaces</h1>
<pre><code>$ ls *mp3 | xargs mplayer
Playing Lemon.
File not found: 'Lemon'
Playing Tree.mp3.
File not found: 'Tree.mp3'
Exiting... (End of file)
</code></pre>
<p>My command fails because the file "Lemon Tree.mp3" contains spaces and so xargs think... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 383 | bash | # Make xargs handle filenames that contain spaces
```
$ ls *mp3 | xargs mplayer
Playing Lemon.
File not found: 'Lemon'
Playing Tree.mp3.
File not found: 'Tree.mp3'
Exiting... (End of file)
```
My command fails because the file "Lemon Tree.mp3" contains spaces and so xargs thinks it's two files. Can I make... | The `xargs` command takes white space characters (tabs, spaces, new lines) as delimiters.
You can narrow it down only for the new line characters ('\n') with `-d` option like this:
```
ls *.mp3 | xargs -d '\n' mplayer
```
It works only with GNU xargs.
For MacOS:
```
ls *.mp3 | tr \\n \\0 | xargs -0 mplayer
```
Th... |
3826425 | How to represent multiple conditions in a shell if statement? | 417 | 2010-09-29 22:41:32 | <p>I want to represent multiple conditions like this:</p>
<pre><code>if [ ( $g -eq 1 -a "$c" = "123" ) -o ( $g -eq 2 -a "$c" = "456" ) ]
then
echo abc;
else
echo efg;
fi
</code></pre>
<p>but when I execute the script, it shows</p>
<pre class="lang-none prett... | 1,205,892 | 389,955 | 2022-01-26 19:15:20 | 3,826,462 | 492 | 2010-09-29 22:51:17 | 15,168 | 2015-05-03 15:43:01 | https://stackoverflow.com/q/3826425 | https://stackoverflow.com/a/3826462 | <p>Classic technique (escape metacharacters):</p>
<pre><code>if [ \( "$g" -eq 1 -a "$c" = "123" \) -o \( "$g" -eq 2 -a "$c" = "456" \) ]
then echo abc
else echo efg
fi
</code></pre>
<p>I've enclosed the references to <code>$g</code> in double quotes; that's good practice, in general. Strictly, the parentheses aren't... | <p>Classic technique (escape metacharacters):</p> <pre><code>if [ \( "$g" -eq 1 -a "$c" = "123" \) -o \( "$g" -eq 2 -a "$c" = "456" \) ] then echo abc else echo efg fi </code></pre> <p>I've enclosed the references to <code>$g</code> in double quotes; that's good practice, in general. Strictly, the parentheses aren't... | 34, 387, 390 | bash, shell, unix | <h1>How to represent multiple conditions in a shell if statement?</h1>
<p>I want to represent multiple conditions like this:</p>
<pre><code>if [ ( $g -eq 1 -a "$c" = "123" ) -o ( $g -eq 2 -a "$c" = "456" ) ]
then
echo abc;
else
echo efg;
fi
</code></pre>
<p>... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 384 | bash | # How to represent multiple conditions in a shell if statement?
I want to represent multiple conditions like this:
```
if [ ( $g -eq 1 -a "$c" = "123" ) -o ( $g -eq 2 -a "$c" = "456" ) ]
then
echo abc;
else
echo efg;
fi
```
but when I execute the script, it shows
```
syntax error at line 15: `['... | Classic technique (escape metacharacters):
```
if [ \( "$g" -eq 1 -a "$c" = "123" \) -o \( "$g" -eq 2 -a "$c" = "456" \) ]
then echo abc
else echo efg
fi
```
I've enclosed the references to `$g` in double quotes; that's good practice, in general. Strictly, the parentheses aren't needed because the precedence of `-a` ... |
1015678 | Get most recent file in a directory on Linux | 346 | 2009-06-18 23:15:19 | <p>Looking for a command that will return the single most recent file in a directory.</p>
<p>Not seeing a limit parameter to <code>ls</code>...</p>
| 543,309 | 32,840 | 2025-07-11 02:20:32 | 1,015,684 | 492 | 2009-06-18 23:17:57 | 2,509 | 2025-07-11 02:08:34 | https://stackoverflow.com/q/1015678 | https://stackoverflow.com/a/1015684 | <pre><code>ls -Art | tail -n 1
</code></pre>
<p>returns the latest modified file or directory.</p>
<p>Flags:</p>
<p><code>-A</code> list all files except <code>.</code> and <code>..</code></p>
<p><code>-r</code> reverse order while sorting</p>
<p><code>-t</code> sort by time, newest first</p>
| <pre><code>ls -Art | tail -n 1 </code></pre> <p>returns the latest modified file or directory.</p> <p>Flags:</p> <p><code>-A</code> list all files except <code>.</code> and <code>..</code></p> <p><code>-r</code> reverse order while sorting</p> <p><code>-t</code> sort by time, newest first</p> | 58, 390, 1231 | command-line, linux, shell | <h1>Get most recent file in a directory on Linux</h1>
<p>Looking for a command that will return the single most recent file in a directory.</p>
<p>Not seeing a limit parameter to <code>ls</code>...</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 385 | bash | # Get most recent file in a directory on Linux
Looking for a command that will return the single most recent file in a directory.
Not seeing a limit parameter to `ls`... | ```
ls -Art | tail -n 1
```
returns the latest modified file or directory.
Flags:
`-A` list all files except `.` and `..`
`-r` reverse order while sorting
`-t` sort by time, newest first |
12524308 | Bash: Strip trailing linebreak from output | 360 | 2012-09-21 04:34:38 | <p>When I execute commands in Bash (or to be specific, <code>wc -l < log.txt</code>), the output contains a linebreak after it. How do I get rid of it?</p>
| 417,700 | 1,380,918 | 2021-07-26 08:24:49 | 12,524,345 | 491 | 2012-09-21 04:39:19 | 751,863 | 2020-10-25 20:35:35 | https://stackoverflow.com/q/12524308 | https://stackoverflow.com/a/12524345 | <p><strong>If your expected output is a single line</strong>, you can simply remove all newline characters from the output. It would not be uncommon to pipe to the <code>tr</code> utility, or to Perl if preferred:</p>
<pre><code>wc -l < log.txt | tr -d '\n'
wc -l < log.txt | perl -pe 'chomp'
</code></pre>
<p>You... | <p><strong>If your expected output is a single line</strong>, you can simply remove all newline characters from the output. It would not be uncommon to pipe to the <code>tr</code> utility, or to Perl if preferred:</p> <pre><code>wc -l < log.txt | tr -d '\n' wc -l < log.txt | perl -pe 'chomp' </code></pre> <p>You... | 387, 3705, 9313 | bash, line-breaks, newline | <h1>Bash: Strip trailing linebreak from output</h1>
<p>When I execute commands in Bash (or to be specific, <code>wc -l < log.txt</code>), the output contains a linebreak after it. How do I get rid of it?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 386 | bash | # Bash: Strip trailing linebreak from output
When I execute commands in Bash (or to be specific, `wc -l < log.txt`), the output contains a linebreak after it. How do I get rid of it? | **If your expected output is a single line**, you can simply remove all newline characters from the output. It would not be uncommon to pipe to the `tr` utility, or to Perl if preferred:
```
wc -l < log.txt | tr -d '\n'
wc -l < log.txt | perl -pe 'chomp'
```
You can also use command substitution to remove the traili... |
18960689 | Running my program says "bash: ./program Permission denied" | 259 | 2013-09-23 13:35:35 | <p>I am running Ubuntu on computer 1 and computer 2. I compiled a C++ program on computer 1, and I can execute it from the terminal using <code>./program_name</code>. It runs fine.</p>
<p>However, when I try to do this on computer 2, it says: <code>bash: ./program_name: permission denied</code></p>
<p>What's wrong an... | 916,775 | 2,432,701 | 2022-07-09 23:38:43 | 18,960,752 | 491 | 2013-09-23 13:39:16 | 298,225 | 2018-06-01 21:33:54 | https://stackoverflow.com/q/18960689 | https://stackoverflow.com/a/18960752 | <p><code>chmod u+x program_name</code>. Then execute it.</p>
<p>If that does not work, copy the program from the USB device to a native volume on the system. Then <code>chmod u+x program_name</code> on the local copy and execute that.</p>
<p>Unix and Unix-like systems generally will not execute a program unless it is... | <p><code>chmod u+x program_name</code>. Then execute it.</p> <p>If that does not work, copy the program from the USB device to a native volume on the system. Then <code>chmod u+x program_name</code> on the local copy and execute that.</p> <p>Unix and Unix-like systems generally will not execute a program unless it is... | 10, 387, 390 | bash, c++, shell | <h1>Running my program says "bash: ./program Permission denied"</h1>
<p>I am running Ubuntu on computer 1 and computer 2. I compiled a C++ program on computer 1, and I can execute it from the terminal using <code>./program_name</code>. It runs fine.</p>
<p>However, when I try to do this on computer 2, it says: <code>b... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 387 | bash | # Running my program says "bash: ./program Permission denied"
I am running Ubuntu on computer 1 and computer 2. I compiled a C++ program on computer 1, and I can execute it from the terminal using `./program_name`. It runs fine.
However, when I try to do this on computer 2, it says: `bash: ./program_name: permission ... | `chmod u+x program_name`. Then execute it.
If that does not work, copy the program from the USB device to a native volume on the system. Then `chmod u+x program_name` on the local copy and execute that.
Unix and Unix-like systems generally will not execute a program unless it is marked with permission to execute. The... |
6438896 | Sorting data based on second column of a file | 328 | 2011-06-22 11:15:48 | <p>I have a file of 2 columns and <code>n</code> number of rows.</p>
<p>column1 contains <code>names</code> and column2 <code>age</code>.</p>
<p>I want to sort the content of this file in ascending order based on the <code>age</code> (in second column).</p>
<p>The result should display the <code>name</code> of the youn... | 356,402 | 735,764 | 2022-09-22 06:21:24 | 6,438,940 | 490 | 2011-06-22 11:18:37 | 143,336 | 2021-12-02 18:31:56 | https://stackoverflow.com/q/6438896 | https://stackoverflow.com/a/6438940 | <p>You can use the <code>key</code> option of the <a href="https://linux.die.net/man/1/sort" rel="noreferrer"><code>sort</code> command</a>, which takes a "field number", so if you wanted the second column:</p>
<pre><code>sort -k2 -n yourfile
</code></pre>
<blockquote>
<p><code>-n</code>, <code>--numeric-sort... | <p>You can use the <code>key</code> option of the <a href="https://linux.die.net/man/1/sort" rel="noreferrer"><code>sort</code> command</a>, which takes a "field number", so if you wanted the second column:</p> <pre><code>sort -k2 -n yourfile </code></pre> <blockquote> <p><code>-n</code>, <code>--numeric-sort... | 34, 387, 390 | bash, shell, unix | <h1>Sorting data based on second column of a file</h1>
<p>I have a file of 2 columns and <code>n</code> number of rows.</p>
<p>column1 contains <code>names</code> and column2 <code>age</code>.</p>
<p>I want to sort the content of this file in ascending order based on the <code>age</code> (in second column).</p>
<p>The ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 388 | bash | # Sorting data based on second column of a file
I have a file of 2 columns and `n` number of rows.
column1 contains `names` and column2 `age`.
I want to sort the content of this file in ascending order based on the `age` (in second column).
The result should display the `name` of the youngest person along with `nam... | You can use the `key` option of the [`sort` command](https://linux.die.net/man/1/sort), which takes a "field number", so if you wanted the second column:
```
sort -k2 -n yourfile
```
> `-n`, `--numeric-sort` compare according to string numerical value
For example:
```
$ cat ages.txt
Bob 12
Jane 48
Mark 3
Tashi 54
... |
13781216 | Meaning of "[: too many arguments" error from if [] (square brackets) | 308 | 2012-12-08 19:46:25 | <p>I couldn't find any one simple straightforward resource spelling out the meaning of and fix for the following BASH shell error, so I'm posting what I found after researching it.</p>
<p><strong>The error:</strong></p>
<pre><code>-bash: [: too many arguments
</code></pre>
<p><strong>Google-friendly version:</strong... | 453,037 | 568,458 | 2021-06-02 15:29:43 | 13,781,217 | 488 | 2012-12-08 19:46:25 | 568,458 | 2019-09-21 17:30:17 | https://stackoverflow.com/q/13781216 | https://stackoverflow.com/a/13781217 | <p>If your <code>$VARIABLE</code> is a string containing spaces or other special characters, <a href="https://serverfault.com/questions/52034/what-is-the-difference-between-double-and-single-square-brackets-in-bash">and single square brackets are used</a> (which is a shortcut for the <code>test</code> command), then th... | <p>If your <code>$VARIABLE</code> is a string containing spaces or other special characters, <a href="https://serverfault.com/questions/52034/what-is-the-difference-between-double-and-single-square-brackets-in-bash">and single square brackets are used</a> (which is a shortcut for the <code>test</code> command), then th... | 387, 2313, 2773 | arguments, bash, if-statement | <h1>Meaning of "[: too many arguments" error from if [] (square brackets)</h1>
<p>I couldn't find any one simple straightforward resource spelling out the meaning of and fix for the following BASH shell error, so I'm posting what I found after researching it.</p>
<p><strong>The error:</strong></p>
<pre><code>-bash: [... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 389 | bash | # Meaning of "[: too many arguments" error from if [] (square brackets)
I couldn't find any one simple straightforward resource spelling out the meaning of and fix for the following BASH shell error, so I'm posting what I found after researching it.
**The error:**
```
-bash: [: too many arguments
```
**Google-frien... | If your `$VARIABLE` is a string containing spaces or other special characters, [and single square brackets are used](https://serverfault.com/questions/52034/what-is-the-difference-between-double-and-single-square-brackets-in-bash) (which is a shortcut for the `test` command), then the string may be split out into multi... |
8779951 | How do I run a shell script without using "sh" or "bash" commands? | 295 | 2012-01-08 18:14:55 | <p>I have a shell script which I want to run without using the "sh" or "bash" commands. For example:</p>
<p>Instead of: <code>sh script.sh</code></p>
<p>I want to use: <code>script.sh</code></p>
<p>How can I do this?</p>
<p>P.S. (i) I don't use shell script much and I tried reading about aliases, but I did not unde... | 667,600 | 1,082,181 | 2022-09-19 03:07:48 | 8,779,980 | 486 | 2012-01-08 18:18:03 | 1,093,528 | 2012-01-08 18:18:03 | https://stackoverflow.com/q/8779951 | https://stackoverflow.com/a/8779980 | <p>Add a "shebang" at the top of your file:</p>
<pre><code>#!/bin/bash
</code></pre>
<p>And make your file executable (<code>chmod +x script.sh</code>).</p>
<p>Finally, modify your path to add the directory where your script is located:</p>
<pre><code>export PATH=$PATH:/appropriate/directory
</code></pre>
<p>(typi... | <p>Add a "shebang" at the top of your file:</p> <pre><code>#!/bin/bash </code></pre> <p>And make your file executable (<code>chmod +x script.sh</code>).</p> <p>Finally, modify your path to add the directory where your script is located:</p> <pre><code>export PATH=$PATH:/appropriate/directory </code></pre> <p>(typi... | 387, 390, 1448, 10327 | alias, bash, sh, shell | <h1>How do I run a shell script without using "sh" or "bash" commands?</h1>
<p>I have a shell script which I want to run without using the "sh" or "bash" commands. For example:</p>
<p>Instead of: <code>sh script.sh</code></p>
<p>I want to use: <code>script.sh</code></p>
<p>How can I do this?</p>
<p>P.S. (i) I don't... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 390 | bash | # How do I run a shell script without using "sh" or "bash" commands?
I have a shell script which I want to run without using the "sh" or "bash" commands. For example:
Instead of: `sh script.sh`
I want to use: `script.sh`
How can I do this?
P.S. (i) I don't use shell script much and I tried reading about aliases, b... | Add a "shebang" at the top of your file:
```
#!/bin/bash
```
And make your file executable (`chmod +x script.sh`).
Finally, modify your path to add the directory where your script is located:
```
export PATH=$PATH:/appropriate/directory
```
(typically, you want `$HOME/bin` for storing your own scripts) |
2022326 | Terminating a script in PowerShell | 583 | 2010-01-07 17:42:14 | <p>I've been looking for a way to terminate a PowerShell (PS1) script when an unrecoverable error occurs within a function. For example:</p>
<pre><code>function foo() {
# Do stuff that causes an error
$host.Exit()
}
</code></pre>
<p>Of course there's no such thing as <code>$host.Exit()</code>. There is <code>$h... | 1,519,154 | 217,219 | 2024-11-27 13:27:44 | 2,022,469 | 485 | 2010-01-07 18:04:58 | 50,356 | 2016-06-29 08:14:39 | https://stackoverflow.com/q/2022326 | https://stackoverflow.com/a/2022469 | <p>You should use <a href="https://stackoverflow.com/questions/1275090/what-exactly-is-exit-in-powershell/1275261#1275261">the <code>exit</code> keyword</a>.</p>
| <p>You should use <a href="https://stackoverflow.com/questions/1275090/what-exactly-is-exit-in-powershell/1275261#1275261">the <code>exit</code> keyword</a>.</p> | 526 | powershell | <h1>Terminating a script in PowerShell</h1>
<p>I've been looking for a way to terminate a PowerShell (PS1) script when an unrecoverable error occurs within a function. For example:</p>
<pre><code>function foo() {
# Do stuff that causes an error
$host.Exit()
}
</code></pre>
<p>Of course there's no such thing as ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 391 | bash | # Terminating a script in PowerShell
I've been looking for a way to terminate a PowerShell (PS1) script when an unrecoverable error occurs within a function. For example:
```
function foo() {
# Do stuff that causes an error
$host.Exit()
}
```
Of course there's no such thing as `$host.Exit()`. There is `$host... | You should use [the `exit` keyword](https://stackoverflow.com/questions/1275090/what-exactly-is-exit-in-powershell/1275261#1275261). |
7690994 | Running a command as Administrator using PowerShell? | 454 | 2011-10-07 17:53:02 | <p>You know how if you're the administrative user of a system and you can just right click say, a batch script and run it as Administrator without entering the administrator password?</p>
<p>I'm wondering how to do this with a PowerShell script. I do not want to have to enter my password; I just want to mimic the righ... | 1,595,131 | 921,587 | 2024-07-01 16:50:11 | 7,691,218 | 485 | 2011-10-07 18:12:29 | 9,833 | 2021-07-10 21:22:52 | https://stackoverflow.com/q/7690994 | https://stackoverflow.com/a/7691218 | <p>If the current console is not elevated and the operation you're trying to do requires elevated privileges then you can start <code>powershell</code> with the <strong>Run as Administrator</strong> option :</p>
<pre><code>PS> Start-Process powershell -Verb runAs
</code></pre>
<p><a href="https://learn.microsoft.com... | <p>If the current console is not elevated and the operation you're trying to do requires elevated privileges then you can start <code>powershell</code> with the <strong>Run as Administrator</strong> option :</p> <pre><code>PS> Start-Process powershell -Verb runAs </code></pre> <p><a href="https://learn.microsoft.com... | 526, 5623 | administrator, powershell | <h1>Running a command as Administrator using PowerShell?</h1>
<p>You know how if you're the administrative user of a system and you can just right click say, a batch script and run it as Administrator without entering the administrator password?</p>
<p>I'm wondering how to do this with a PowerShell script. I do not wa... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 392 | bash | # Running a command as Administrator using PowerShell?
You know how if you're the administrative user of a system and you can just right click say, a batch script and run it as Administrator without entering the administrator password?
I'm wondering how to do this with a PowerShell script. I do not want to have to en... | If the current console is not elevated and the operation you're trying to do requires elevated privileges then you can start `powershell` with the **Run as Administrator** option :
```
PS> Start-Process powershell -Verb runAs
```
[Microsoft Docs: Start-Process](https://learn.microsoft.com/powershell/module/Microsoft.... |
6916856 | Can bash show a function's definition? | 373 | 2011-08-02 18:35:07 | <p>Is there a way to view a bash function's definition in bash?</p>
<p>For example, say I defined the function <code>foobar</code></p>
<pre><code>function foobar {
echo "I'm foobar"
}
</code></pre>
<p>Is there any way to later get the code that <code>foobar</code> runs?</p>
<pre><code>$ # non-working pseudocode... | 76,143 | 594,496 | 2018-12-04 08:52:58 | 6,916,952 | 485 | 2011-08-02 18:43:03 | 176,922 | 2018-12-04 08:52:58 | https://stackoverflow.com/q/6916856 | https://stackoverflow.com/a/6916952 | <p>Use <code>type</code>. If <code>foobar</code> is e.g. defined in your <code>~/.profile</code>:</p>
<pre><code>$ type foobar
foobar is a function
foobar {
echo "I'm foobar"
}
</code></pre>
<p>This does find out what <code>foobar</code> was, and if it was defined as a function it calls <code>declare -f</code> as... | <p>Use <code>type</code>. If <code>foobar</code> is e.g. defined in your <code>~/.profile</code>:</p> <pre><code>$ type foobar foobar is a function foobar { echo "I'm foobar" } </code></pre> <p>This does find out what <code>foobar</code> was, and if it was defined as a function it calls <code>declare -f</code> as... | 387, 5569 | bash, function | <h1>Can bash show a function's definition?</h1>
<p>Is there a way to view a bash function's definition in bash?</p>
<p>For example, say I defined the function <code>foobar</code></p>
<pre><code>function foobar {
echo "I'm foobar"
}
</code></pre>
<p>Is there any way to later get the code that <code>foobar</code> ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 393 | bash | # Can bash show a function's definition?
Is there a way to view a bash function's definition in bash?
For example, say I defined the function `foobar`
```
function foobar {
echo "I'm foobar"
}
```
Is there any way to later get the code that `foobar` runs?
```
$ # non-working pseudocode
$ echo $foobar
echo "I'm... | Use `type`. If `foobar` is e.g. defined in your `~/.profile`:
```
$ type foobar
foobar is a function
foobar {
echo "I'm foobar"
}
```
This does find out what `foobar` was, and if it was defined as a function it calls `declare -f` as explained by pmohandras.
To print out just the body of the function (i.e. the co... |
3637668 | Why are scripting languages (e.g. Perl, Python, and Ruby) not suitable as shell languages? | 355 | 2010-09-03 16:24:58 | <p>What are the differences between shell languages like <a href="http://www.gnu.org/software/bash/bash.html" rel="noreferrer">Bash</a> (<code>bash</code>), <a href="http://zsh.sourceforge.net/" rel="noreferrer">Z shell</a> (<code>zsh</code>), <a href="http://fishshell.com/screenshots.html" rel="noreferrer">Fish</a> (<... | 69,463 | 336,455 | 2022-08-01 14:13:42 | 3,640,403 | 482 | 2010-09-04 00:26:23 | 2,988 | 2013-05-10 16:57:52 | https://stackoverflow.com/q/3637668 | https://stackoverflow.com/a/3640403 | <p>There are a couple of differences that I can think of; just thoughtstreaming here, in no particular order:</p>
<ol>
<li><p>Python & Co. are designed to be good at scripting. Bash & Co. are designed to be <em>only</em> good at scripting, with absolutely no compromise. IOW: Python is designed to be good both ... | <p>There are a couple of differences that I can think of; just thoughtstreaming here, in no particular order:</p> <ol> <li><p>Python & Co. are designed to be good at scripting. Bash & Co. are designed to be <em>only</em> good at scripting, with absolutely no compromise. IOW: Python is designed to be good both ... | 12, 16, 387, 390, 580 | bash, perl, python, ruby, shell | <h1>Why are scripting languages (e.g. Perl, Python, and Ruby) not suitable as shell languages?</h1>
<p>What are the differences between shell languages like <a href="http://www.gnu.org/software/bash/bash.html" rel="noreferrer">Bash</a> (<code>bash</code>), <a href="http://zsh.sourceforge.net/" rel="noreferrer">Z shell<... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 394 | bash | # Why are scripting languages (e.g. Perl, Python, and Ruby) not suitable as shell languages?
What are the differences between shell languages like [Bash](http://www.gnu.org/software/bash/bash.html) (`bash`), [Z shell](http://zsh.sourceforge.net/) (`zsh`), [Fish](http://fishshell.com/screenshots.html) (`fish`) and the ... | There are a couple of differences that I can think of; just thoughtstreaming here, in no particular order:
1. Python & Co. are designed to be good at scripting. Bash & Co. are designed to be *only* good at scripting, with absolutely no compromise. IOW: Python is designed to be good both at scripting and non-scripting,... |
13280131 | Hexadecimal To Decimal in Shell Script | 204 | 2012-11-07 23:31:43 | <p>Can someone help me to convert a hexadecimal number to decimal number in a shell script?</p>
<p>E.g., I want to convert the hexadecimal number <code>bfca3000</code> to decimal using a shell script. I basically want the difference of two hexadecimal numbers.</p>
<p>My code is:</p>
<pre><code>var3=`echo "ibase=16; ... | 268,834 | 1,460,207 | 2025-01-21 21:32:28 | 13,280,173 | 481 | 2012-11-07 23:36:18 | 465,183 | 2025-01-21 21:32:28 | https://stackoverflow.com/q/13280131 | https://stackoverflow.com/a/13280173 | <p>To convert from hex to decimal, there are many ways to do it in the shell or with an external program.</p>
<p>With <a href="/questions/tagged/bash" class="s-tag post-tag" title="show questions tagged 'bash'" aria-label="show questions tagged 'bash'" rel="tag" aria-labelledby="tag-bash-tooltip-contain... | <p>To convert from hex to decimal, there are many ways to do it in the shell or with an external program.</p> <p>With <a href="/questions/tagged/bash" class="s-tag post-tag" title="show questions tagged 'bash'" aria-label="show questions tagged 'bash'" rel="tag" aria-labelledby="tag-bash-tooltip-contain... | 387, 390, 1391, 6250 | bash, decimal, hex, shell | <h1>Hexadecimal To Decimal in Shell Script</h1>
<p>Can someone help me to convert a hexadecimal number to decimal number in a shell script?</p>
<p>E.g., I want to convert the hexadecimal number <code>bfca3000</code> to decimal using a shell script. I basically want the difference of two hexadecimal numbers.</p>
<p>My... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 395 | bash | # Hexadecimal To Decimal in Shell Script
Can someone help me to convert a hexadecimal number to decimal number in a shell script?
E.g., I want to convert the hexadecimal number `bfca3000` to decimal using a shell script. I basically want the difference of two hexadecimal numbers.
My code is:
```
var3=`echo "ibase=1... | To convert from hex to decimal, there are many ways to do it in the shell or with an external program.
With [bash](/questions/tagged/bash "show questions tagged 'bash'"):
```
$ echo $((16#FF))
255
```
with [bc](/questions/tagged/bc "show questions tagged 'bc'"):
```
$ echo "ibase=16; FF" | bc
255
```
with [perl](/... |
816820 | Use space as a delimiter with cut command | 441 | 2009-05-03 11:24:23 | <p>I want to use space as a delimiter with the <code>cut</code> command.</p>
<p>What syntax can I use for this?</p>
| 482,992 | 92,682 | 2023-09-07 19:33:48 | 816,824 | 480 | 2009-05-03 11:28:19 | 21,886 | 2009-05-03 11:28:19 | https://stackoverflow.com/q/816820 | https://stackoverflow.com/a/816824 | <pre><code>cut -d ' ' -f 2
</code></pre>
<p>Where 2 is the field number of the space-delimited field you want.</p>
| <pre><code>cut -d ' ' -f 2 </code></pre> <p>Where 2 is the field number of the space-delimited field you want.</p> | 34, 387, 4257 | bash, cut, unix | <h1>Use space as a delimiter with cut command</h1>
<p>I want to use space as a delimiter with the <code>cut</code> command.</p>
<p>What syntax can I use for this?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 396 | bash | # Use space as a delimiter with cut command
I want to use space as a delimiter with the `cut` command.
What syntax can I use for this? | ```
cut -d ' ' -f 2
```
Where 2 is the field number of the space-delimited field you want. |
584894 | Environment variable substitution in sed | 340 | 2009-02-25 06:10:31 | <p>If I run these commands from a script:</p>
<pre><code>#my.sh
PWD=bla
sed 's/xxx/'$PWD'/'
...
$ ./my.sh
xxx
bla
</code></pre>
<p>it is fine.</p>
<p>But, if I run:</p>
<pre><code>#my.sh
sed 's/xxx/'$PWD'/'
...
$ ./my.sh
$ sed: -e expression #1, char 8: Unknown option to `s'
</code></pre>
<p>I read in tutorials t... | 439,426 | 14,587 | 2022-08-30 17:19:51 | 584,926 | 479 | 2009-02-25 06:21:34 | 41,661 | 2017-05-05 14:52:45 | https://stackoverflow.com/q/584894 | https://stackoverflow.com/a/584926 | <p>Your two examples look identical, which makes problems hard to diagnose. Potential problems:</p>
<ol>
<li><p>You may need double quotes, as in <code>sed 's/xxx/'"$PWD"'/'</code></p></li>
<li><p><code>$PWD</code> may contain a slash, in which case you need to find a character <em>not</em> contained in <code>$PWD</c... | <p>Your two examples look identical, which makes problems hard to diagnose. Potential problems:</p> <ol> <li><p>You may need double quotes, as in <code>sed 's/xxx/'"$PWD"'/'</code></p></li> <li><p><code>$PWD</code> may contain a slash, in which case you need to find a character <em>not</em> contained in <code>$PWD</c... | 34, 58, 390, 5282 | linux, sed, shell, unix | <h1>Environment variable substitution in sed</h1>
<p>If I run these commands from a script:</p>
<pre><code>#my.sh
PWD=bla
sed 's/xxx/'$PWD'/'
...
$ ./my.sh
xxx
bla
</code></pre>
<p>it is fine.</p>
<p>But, if I run:</p>
<pre><code>#my.sh
sed 's/xxx/'$PWD'/'
...
$ ./my.sh
$ sed: -e expression #1, char 8: Unknown opti... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 397 | bash | # Environment variable substitution in sed
If I run these commands from a script:
```
#my.sh
PWD=bla
sed 's/xxx/'$PWD'/'
...
$ ./my.sh
xxx
bla
```
it is fine.
But, if I run:
```
#my.sh
sed 's/xxx/'$PWD'/'
...
$ ./my.sh
$ sed: -e expression #1, char 8: Unknown option to `s'
```
I read in tutorials that to substitu... | Your two examples look identical, which makes problems hard to diagnose. Potential problems:
1. You may need double quotes, as in `sed 's/xxx/'"$PWD"'/'`
2. `$PWD` may contain a slash, in which case you need to find a character *not* contained in `$PWD` to use as a delimiter.
To nail both issues at once, perhaps
```... |
2642585 | Read a variable in bash with a default value | 309 | 2010-04-15 03:41:39 | <p>I need to read a value from the terminal in a bash script. I would like to be able to provide a default value that the user can change.</p>
<pre><code># Please enter your name: Ricardo^
</code></pre>
<p>In this script the prompt is "Please enter your name: " the default value is "Ricardo" and the cursor would be ... | 289,079 | 103,260 | 2021-12-28 22:11:30 | 2,642,592 | 478 | 2010-04-15 03:45:31 | 131,527 | 2020-08-18 01:01:47 | https://stackoverflow.com/q/2642585 | https://stackoverflow.com/a/2642592 | <p>You can use <a href="https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html" rel="noreferrer">parameter expansion</a>, e.g.</p>
<pre><code>read -p "Enter your name [Richard]: " name
name=${name:-Richard}
echo $name
</code></pre>
<p>Including the default value in the prompt betwee... | <p>You can use <a href="https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html" rel="noreferrer">parameter expansion</a>, e.g.</p> <pre><code>read -p "Enter your name [Richard]: " name name=${name:-Richard} echo $name </code></pre> <p>Including the default value in the prompt betwee... | 387, 390 | bash, shell | <h1>Read a variable in bash with a default value</h1>
<p>I need to read a value from the terminal in a bash script. I would like to be able to provide a default value that the user can change.</p>
<pre><code># Please enter your name: Ricardo^
</code></pre>
<p>In this script the prompt is "Please enter your name: " t... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 398 | bash | # Read a variable in bash with a default value
I need to read a value from the terminal in a bash script. I would like to be able to provide a default value that the user can change.
```
# Please enter your name: Ricardo^
```
In this script the prompt is "Please enter your name: " the default value is "Ricardo" and ... | You can use [parameter expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html), e.g.
```
read -p "Enter your name [Richard]: " name
name=${name:-Richard}
echo $name
```
Including the default value in the prompt between brackets is a fairly common convention
What does the `:-Rich... |
4936807 | How to set ssh timeout? | 302 | 2011-02-08 18:25:37 | <p>I'm executing a script connecting via password-less SSH on a remote host. I want to set a timeout, so that if the remote host is taking an infinite time to run, I want to come out of that ssh session and continue other lines in my sh script.</p>
<p>How can I set a timeout?</p>
| 474,768 | 57,421 | 2024-10-25 05:40:09 | 5,255,550 | 478 | 2011-03-10 04:41:52 | 57,421 | 2020-03-07 03:05:43 | https://stackoverflow.com/q/4936807 | https://stackoverflow.com/a/5255550 | <pre><code>ssh -o ConnectTimeout=10 <hostName>
</code></pre>
<p>Where 10 is time in seconds. This Timeout applies only to the creation of the connection.</p>
| <pre><code>ssh -o ConnectTimeout=10 <hostName> </code></pre> <p>Where 10 is time in seconds. This Timeout applies only to the creation of the connection.</p> | 386, 390, 2702 | shell, ssh, timeout | <h1>How to set ssh timeout?</h1>
<p>I'm executing a script connecting via password-less SSH on a remote host. I want to set a timeout, so that if the remote host is taking an infinite time to run, I want to come out of that ssh session and continue other lines in my sh script.</p>
<p>How can I set a timeout?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 399 | bash | # How to set ssh timeout?
I'm executing a script connecting via password-less SSH on a remote host. I want to set a timeout, so that if the remote host is taking an infinite time to run, I want to come out of that ssh session and continue other lines in my sh script.
How can I set a timeout? | ```
ssh -o ConnectTimeout=10 <hostName>
```
Where 10 is time in seconds. This Timeout applies only to the creation of the connection. |
18544359 | How do I read user input into a variable in Bash? | 262 | 2013-08-31 04:03:08 | <p>How do I read user input into a variable in Bash?</p>
<pre class="lang-bash prettyprint-override"><code>fullname=""
# Now, read user input into the variable `fullname`.
</code></pre>
| 462,309 | 2,602,638 | 2025-07-07 22:02:57 | 18,546,416 | 478 | 2013-08-31 09:05:18 | 445,221 | 2024-04-26 03:25:48 | https://stackoverflow.com/q/18544359 | https://stackoverflow.com/a/18546416 | <p>Use <a href="https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-read" rel="noreferrer"><code>read -p</code></a>:</p>
<pre><code># fullname="USER INPUT"
read -p "Enter fullname: " fullname
# user="USER INPUT"
read -p "Enter user: " user
</code></pre>
<p... | <p>Use <a href="https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-read" rel="noreferrer"><code>read -p</code></a>:</p> <pre><code># fullname="USER INPUT" read -p "Enter fullname: " fullname # user="USER INPUT" read -p "Enter user: " user </code></pre> <p... | 387, 390, 5486 | bash, input, shell | <h1>How do I read user input into a variable in Bash?</h1>
<p>How do I read user input into a variable in Bash?</p>
<pre class="lang-bash prettyprint-override"><code>fullname=""
# Now, read user input into the variable `fullname`.
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 400 | bash | # How do I read user input into a variable in Bash?
How do I read user input into a variable in Bash?
```
fullname=""
# Now, read user input into the variable `fullname`.
``` | Use [`read -p`](https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-read):
```
# fullname="USER INPUT"
read -p "Enter fullname: " fullname
# user="USER INPUT"
read -p "Enter user: " user
```
If you like to get the user's confirmation:
```
read -p "Continue? (Y/N): " confirm && [[ $confirm == ... |
16548528 | Linux command to get time in milliseconds | 474 | 2013-05-14 16:42:00 | <p>How can I get time in milliseconds in a Bash shell script?</p>
| 654,733 | 1,003,575 | 2024-09-02 13:41:32 | 16,548,827 | 473 | 2013-05-14 17:00:41 | 2,328,763 | 2019-11-18 13:09:17 | https://stackoverflow.com/q/16548528 | https://stackoverflow.com/a/16548827 | <p><code>date +%s%N</code> returns the number of seconds + current nanoseconds.</p>
<p>Therefore, <code>echo $(($(date +%s%N)/1000000))</code> is what you need.</p>
<p>Example:</p>
<pre><code>$ echo $(($(date +%s%N)/1000000))
1535546718115
</code></pre>
<p><code>date +%s</code> returns the number of seconds since t... | <p><code>date +%s%N</code> returns the number of seconds + current nanoseconds.</p> <p>Therefore, <code>echo $(($(date +%s%N)/1000000))</code> is what you need.</p> <p>Example:</p> <pre><code>$ echo $(($(date +%s%N)/1000000)) 1535546718115 </code></pre> <p><code>date +%s</code> returns the number of seconds since t... | 58, 387, 390, 603 | bash, linux, shell, time | <h1>Linux command to get time in milliseconds</h1>
<p>How can I get time in milliseconds in a Bash shell script?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 401 | bash | # Linux command to get time in milliseconds
How can I get time in milliseconds in a Bash shell script? | `date +%s%N` returns the number of seconds + current nanoseconds.
Therefore, `echo $(($(date +%s%N)/1000000))` is what you need.
Example:
```
$ echo $(($(date +%s%N)/1000000))
1535546718115
```
`date +%s` returns the number of seconds since the epoch, if that's useful. |
1335815 | How to slice an array in Bash | 284 | 2009-08-26 16:04:12 | <p>Looking the "Array" section in the bash(1) man page, I didn't find a way to slice an array.</p>
<p>So I came up with this overly complicated function:</p>
<pre><code>#!/bin/bash
# @brief: slice a bash array
# @arg1: output-name
# @arg2: input-name
# @args: seq args
# -------------------------------------------... | 196,078 | 110,488 | 2021-07-18 19:29:49 | 1,336,245 | 473 | 2009-08-26 17:10:33 | 26,428 | 2021-05-27 14:14:48 | https://stackoverflow.com/q/1335815 | https://stackoverflow.com/a/1336245 | <p>See the <a href="https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion" rel="noreferrer">Parameter Expansion</a> section in the Bash <code>man</code> page. <code>A[@]</code> returns the contents of the array, <code>:1:2</code> takes a slice of length 2, starting at index 1.</p>
<pre><code>A=( ... | <p>See the <a href="https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion" rel="noreferrer">Parameter Expansion</a> section in the Bash <code>man</code> page. <code>A[@]</code> returns the contents of the array, <code>:1:2</code> takes a slice of length 2, starting at index 1.</p> <pre><code>A=( ... | 114, 387, 20851 | arrays, bash, slice | <h1>How to slice an array in Bash</h1>
<p>Looking the "Array" section in the bash(1) man page, I didn't find a way to slice an array.</p>
<p>So I came up with this overly complicated function:</p>
<pre><code>#!/bin/bash
# @brief: slice a bash array
# @arg1: output-name
# @arg2: input-name
# @args: seq args
# ----... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 402 | bash | # How to slice an array in Bash
Looking the "Array" section in the bash(1) man page, I didn't find a way to slice an array.
So I came up with this overly complicated function:
```
#!/bin/bash
# @brief: slice a bash array
# @arg1: output-name
# @arg2: input-name
# @args: seq args
# -------------------------------... | See the [Parameter Expansion](https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion) section in the Bash `man` page. `A[@]` returns the contents of the array, `:1:2` takes a slice of length 2, starting at index 1.
```
A=( foo bar "a b c" 42 )
B=("${A[@]:1:2}")
C=("${A[@]:1}") # slice to t... |
7787029 | How do I rename all files to lowercase? | 157 | 2011-10-16 20:18:24 | <p>I have for example TREE.wav, ONE.WAV. I want to rename it to tree.wav, one.wav. How do I rename all files to lowercase? </p>
| 82,936 | 499,825 | 2025-10-11 14:06:04 | 7,787,159 | 473 | 2011-10-16 20:39:27 | 446,843 | 2016-06-04 07:12:01 | https://stackoverflow.com/q/7787029 | https://stackoverflow.com/a/7787159 | <p>If you're comfortable with the terminal:</p>
<ol>
<li>Open Terminal.app, type <code>cd</code> and then drag and drop the Folder containing the files to be renamed into the window.</li>
<li>To confirm you're in the correct directory, type <code>ls</code> and hit enter.</li>
<li><p>Paste this code and hit enter:</p>
... | <p>If you're comfortable with the terminal:</p> <ol> <li>Open Terminal.app, type <code>cd</code> and then drag and drop the Folder containing the files to be renamed into the window.</li> <li>To confirm you're in the correct directory, type <code>ls</code> and hit enter.</li> <li><p>Paste this code and hit enter:</p> ... | 369, 387, 5310 | bash, file, macos | <h1>How do I rename all files to lowercase?</h1>
<p>I have for example TREE.wav, ONE.WAV. I want to rename it to tree.wav, one.wav. How do I rename all files to lowercase? </p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 403 | bash | # How do I rename all files to lowercase?
I have for example TREE.wav, ONE.WAV. I want to rename it to tree.wav, one.wav. How do I rename all files to lowercase? | If you're comfortable with the terminal:
1. Open Terminal.app, type `cd` and then drag and drop the Folder containing the files to be renamed into the window.
2. To confirm you're in the correct directory, type `ls` and hit enter.
3. Paste this code and hit enter:
```
for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp... |
6883760 | Git for Windows: .bashrc or equivalent configuration files for Git Bash shell | 411 | 2011-07-30 14:27:44 | <p>I've just installed Git for Windows and am delighted to see that it installs Bash.</p>
<p>I want to customise the shell in the same way I can under Linux (e.g. set up aliases like <code>ll</code> for <code>ls -l</code>), but I can't seem to find <code>.bashrc</code> or equivalent configuration files.</p>
<p>What s... | 470,389 | 160,604 | 2024-04-01 16:51:02 | 6,883,798 | 472 | 2011-07-30 14:33:33 | 11,708 | 2023-06-27 00:52:28 | https://stackoverflow.com/q/6883760 | https://stackoverflow.com/a/6883798 | <p>Create a <code>.bashrc</code> file under <code>~/.bashrc</code> and away you go. Similarly for <code>~/.gitconfig</code>.</p>
<p><code>~</code> is usually your <code>C:\Users\<your user name></code> folder. Typing <code>echo ~</code> in the Git Bash terminal will tell you what that folder is.</p>
<p>If you can... | <p>Create a <code>.bashrc</code> file under <code>~/.bashrc</code> and away you go. Similarly for <code>~/.gitconfig</code>.</p> <p><code>~</code> is usually your <code>C:\Users\<your user name></code> folder. Typing <code>echo ~</code> in the Git Bash terminal will tell you what that folder is.</p> <p>If you can... | 64, 119, 61874 | git, git-bash, windows | <h1>Git for Windows: .bashrc or equivalent configuration files for Git Bash shell</h1>
<p>I've just installed Git for Windows and am delighted to see that it installs Bash.</p>
<p>I want to customise the shell in the same way I can under Linux (e.g. set up aliases like <code>ll</code> for <code>ls -l</code>), but I ca... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 404 | bash | # Git for Windows: .bashrc or equivalent configuration files for Git Bash shell
I've just installed Git for Windows and am delighted to see that it installs Bash.
I want to customise the shell in the same way I can under Linux (e.g. set up aliases like `ll` for `ls -l`), but I can't seem to find `.bashrc` or equivale... | Create a `.bashrc` file under `~/.bashrc` and away you go. Similarly for `~/.gitconfig`.
`~` is usually your `C:\Users\<your user name>` folder. Typing `echo ~` in the Git Bash terminal will tell you what that folder is.
If you can't create the file (e.g. running Windows), run the below command:
```
copy > ~/.bashrc... |
11616835 | '\r': command not found - .bashrc / .bash_profile | 429 | 2012-07-23 16:45:53 | <p>I have windows, using Cygwin, trying to set <code>JAVA_HOME</code> permanently through my <code>.bashrc</code> file. </p>
<p><strong>.bashrc:</strong></p>
<pre><code>export PATH="$JAVA_HOME/bin:$PATH"
export JAVA_HOME=$JAVA_HOME:"/cygdrive/c/Program Files (x86)/Java/jdk1.7.0_05"
</code></pre>
<p><strong>.bash_p... | 642,703 | 1,420,783 | 2022-10-06 13:05:31 | 11,617,204 | 469 | 2012-07-23 17:10:35 | 778,118 | 2013-06-20 07:33:23 | https://stackoverflow.com/q/11616835 | https://stackoverflow.com/a/11617204 | <p><strong>When all else fails in Cygwin...</strong></p>
<p>Try running the <code>dos2unix</code> command on the file in question.</p>
<p>It might help when you see error messages like this: </p>
<p><code>-bash: '\r': command not found</code></p>
<p>Windows style newline characters can cause issues in Cygwin.</p>
... | <p><strong>When all else fails in Cygwin...</strong></p> <p>Try running the <code>dos2unix</code> command on the file in question.</p> <p>It might help when you see error messages like this: </p> <p><code>-bash: '\r': command not found</code></p> <p>Windows style newline characters can cause issues in Cygwin.</p> ... | 387, 390, 1731, 3705 | bash, cygwin, newline, shell | <h1>'\r': command not found - .bashrc / .bash_profile</h1>
<p>I have windows, using Cygwin, trying to set <code>JAVA_HOME</code> permanently through my <code>.bashrc</code> file. </p>
<p><strong>.bashrc:</strong></p>
<pre><code>export PATH="$JAVA_HOME/bin:$PATH"
export JAVA_HOME=$JAVA_HOME:"/cygdrive/c/Program File... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 405 | bash | # '\r': command not found - .bashrc / .bash_profile
I have windows, using Cygwin, trying to set `JAVA_HOME` permanently through my `.bashrc` file.
**.bashrc:**
```
export PATH="$JAVA_HOME/bin:$PATH"
export JAVA_HOME=$JAVA_HOME:"/cygdrive/c/Program Files (x86)/Java/jdk1.7.0_05"
```
**.bash_profile:**
```
if [ -f ... | **When all else fails in Cygwin...**
Try running the `dos2unix` command on the file in question.
It might help when you see error messages like this:
`-bash: '\r': command not found`
Windows style newline characters can cause issues in Cygwin.
The `dos2unix` command modifies newline characters so they are Unix / C... |
12137431 | Test if a command outputs an empty string | 401 | 2012-08-27 06:53:37 | <p>How can I test if a command outputs an empty string?</p>
| 417,572 | 1,328,804 | 2024-01-31 18:33:36 | 12,137,501 | 466 | 2012-08-27 07:00:15 | 58,635 | 2017-04-26 07:43:40 | https://stackoverflow.com/q/12137431 | https://stackoverflow.com/a/12137501 | <p><sub>Previously, the question asked how to check whether there are files in a directory. The following code achieves that, but see <a href="https://stackoverflow.com/a/35165216/58635">rsp's answer</a> for a better solution.</sub></p>
<hr>
<h2>Empty output</h2>
<p>Commands don’t <em>return</em> values – they outp... | <p><sub>Previously, the question asked how to check whether there are files in a directory. The following code achieves that, but see <a href="https://stackoverflow.com/a/35165216/58635">rsp's answer</a> for a better solution.</sub></p> <hr> <h2>Empty output</h2> <p>Commands don’t <em>return</em> values – they outp... | 387, 390 | bash, shell | <h1>Test if a command outputs an empty string</h1>
<p>How can I test if a command outputs an empty string?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 406 | bash | # Test if a command outputs an empty string
How can I test if a command outputs an empty string? | Previously, the question asked how to check whether there are files in a directory. The following code achieves that, but see [rsp's answer](https://stackoverflow.com/a/35165216/58635) for a better solution.
---
## Empty output
Commands don’t *return* values – they output them. You can capture this output by using *... |
171924 | How do I syntax check a Bash script without running it? | 328 | 2008-10-05 12:51:25 | <p>Is it possible to check a bash script syntax without executing it?</p>
<p>Using Perl, I can run <code>perl -c 'script name'</code>. Is there any equivalent command for bash scripts?</p>
| 311,910 | 13,523 | 2022-02-19 19:57:32 | 171,932 | 464 | 2008-10-05 12:55:00 | 6,152 | 2012-11-15 15:47:06 | https://stackoverflow.com/q/171924 | https://stackoverflow.com/a/171932 | <pre><code>bash -n scriptname
</code></pre>
<p>Perhaps an obvious caveat: this validates syntax but won't check if your bash script tries to execute a command that isn't in your path, like <code>ech hello</code> instead of <code>echo hello</code>.</p>
| <pre><code>bash -n scriptname </code></pre> <p>Perhaps an obvious caveat: this validates syntax but won't check if your bash script tries to execute a command that isn't in your path, like <code>ech hello</code> instead of <code>echo hello</code>.</p> | 34, 58, 367, 387, 4072 | bash, gnu, linux, syntax, unix | <h1>How do I syntax check a Bash script without running it?</h1>
<p>Is it possible to check a bash script syntax without executing it?</p>
<p>Using Perl, I can run <code>perl -c 'script name'</code>. Is there any equivalent command for bash scripts?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 407 | bash | # How do I syntax check a Bash script without running it?
Is it possible to check a bash script syntax without executing it?
Using Perl, I can run `perl -c 'script name'`. Is there any equivalent command for bash scripts? | ```
bash -n scriptname
```
Perhaps an obvious caveat: this validates syntax but won't check if your bash script tries to execute a command that isn't in your path, like `ech hello` instead of `echo hello`. |
9574089 | OS X Bash, 'watch' command | 510 | 2012-03-05 21:15:21 | <p>I'm looking for the best way to duplicate the Linux 'watch' command on Mac OS X. I'd like to run a command every few seconds to pattern match on the contents of an output file using 'tail' and 'sed'.</p>
<p>What's my best option on a Mac, and can it be done without downloading software?</p>
| 292,864 | 1,192,740 | 2023-04-19 18:21:08 | 9,574,123 | 463 | 2012-03-05 21:17:44 | 1,024,637 | 2015-10-22 23:29:11 | https://stackoverflow.com/q/9574089 | https://stackoverflow.com/a/9574123 | <p>You can emulate the basic functionality with the shell loop:</p>
<pre><code>while :; do clear; your_command; sleep 2; done
</code></pre>
<p>That will loop forever, clear the screen, run your command, and wait two seconds - the basic <code>watch your_command</code> implementation.</p>
<p>You can take this a step f... | <p>You can emulate the basic functionality with the shell loop:</p> <pre><code>while :; do clear; your_command; sleep 2; done </code></pre> <p>That will loop forever, clear the screen, run your command, and wait two seconds - the basic <code>watch your_command</code> implementation.</p> <p>You can take this a step f... | 369, 387, 1362, 11274 | automation, bash, macos, watch | <h1>OS X Bash, 'watch' command</h1>
<p>I'm looking for the best way to duplicate the Linux 'watch' command on Mac OS X. I'd like to run a command every few seconds to pattern match on the contents of an output file using 'tail' and 'sed'.</p>
<p>What's my best option on a Mac, and can it be done without downloading sof... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 408 | bash | # OS X Bash, 'watch' command
I'm looking for the best way to duplicate the Linux 'watch' command on Mac OS X. I'd like to run a command every few seconds to pattern match on the contents of an output file using 'tail' and 'sed'.
What's my best option on a Mac, and can it be done without downloading software? | You can emulate the basic functionality with the shell loop:
```
while :; do clear; your_command; sleep 2; done
```
That will loop forever, clear the screen, run your command, and wait two seconds - the basic `watch your_command` implementation.
You can take this a step further and create a `watch.sh` script that ca... |
17794507 | Reload the path in PowerShell | 302 | 2013-07-22 18:14:20 | <p>If I have an instance of PowerShell ISE running and I install something that modifies the PATH or I modify it in any way outside of PowerShell then I need to restart PowerShell for it to see the updated PATH variable. </p>
<p>Is there a way to reload the path from within PowerShell without restarting it?</p>
| 217,283 | 373,655 | 2022-02-19 14:06:10 | 31,845,512 | 463 | 2015-08-06 01:44:59 | 65,387 | 2015-08-06 01:44:59 | https://stackoverflow.com/q/17794507 | https://stackoverflow.com/a/31845512 | <p>Just to bring <a href="https://stackoverflow.com/questions/17794507/reload-the-path-in-powershell#comment25967553_17794885">Rob's comment</a> to light:</p>
<pre><code>$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
</cod... | <p>Just to bring <a href="https://stackoverflow.com/questions/17794507/reload-the-path-in-powershell#comment25967553_17794885">Rob's comment</a> to light:</p> <pre><code>$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") </cod... | 488, 526, 22864 | console, powershell, powershell-ise | <h1>Reload the path in PowerShell</h1>
<p>If I have an instance of PowerShell ISE running and I install something that modifies the PATH or I modify it in any way outside of PowerShell then I need to restart PowerShell for it to see the updated PATH variable. </p>
<p>Is there a way to reload the path from within Power... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 409 | bash | # Reload the path in PowerShell
If I have an instance of PowerShell ISE running and I install something that modifies the PATH or I modify it in any way outside of PowerShell then I need to restart PowerShell for it to see the updated PATH variable.
Is there a way to reload the path from within PowerShell without res... | Just to bring [Rob's comment](https://stackoverflow.com/questions/17794507/reload-the-path-in-powershell#comment25967553_17794885) to light:
```
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
``` |
58280652 | Git doesn't work on MacOS Catalina: "xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing" | 271 | 2019-10-08 05:32:54 | <p>After upgrading to MacOS X 10.15 Catalina, I cannot run any git commands in my shell:</p>
<blockquote>
<p>The default interactive shell is now zsh.</p>
<p>To update your account to use zsh, please run <code>chsh -s /bin/zsh</code>.</p>
<p>For more details, please visit <a href="https://support.apple.com/kb/HT208050"... | 85,423 | null | 2019-10-10 09:25:42 | 58,280,847 | 463 | 2019-10-08 05:56:16 | 347,924 | 2019-10-08 05:56:16 | https://stackoverflow.com/q/58280652 | https://stackoverflow.com/a/58280847 | <p>You'll need to reinstall the command line tools:</p>
<pre><code>$ xcode-select --install
</code></pre>
| <p>You'll need to reinstall the command line tools:</p> <pre><code>$ xcode-select --install </code></pre> | 119, 369, 387, 3791, 138656 | bash, git, macos, macos-catalina, zsh | <h1>Git doesn't work on MacOS Catalina: "xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing"</h1>
<p>After upgrading to MacOS X 10.15 Catalina, I cannot run any git commands in my shell:</p>
<blockquote>
<p>The default interactive shell is now zsh.</p>
<p>To update your account t... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 410 | bash | # Git doesn't work on MacOS Catalina: "xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing"
After upgrading to MacOS X 10.15 Catalina, I cannot run any git commands in my shell:
> The default interactive shell is now zsh.
>
> To update your account to use zsh, please run `chsh -... | You'll need to reinstall the command line tools:
```
$ xcode-select --install
``` |
8206280 | Delete all lines beginning with a # from a file | 282 | 2011-11-21 01:17:18 | <p>All of the lines with comments in a file begin with <code>#</code>. How can I delete all of the lines (and only those lines) which begin with <code>#</code>? Other lines containing <code>#</code>, but not at the beginning of the line should be ignored.</p>
| 330,133 | 834,616 | 2025-03-08 01:40:39 | 8,206,295 | 462 | 2011-11-21 01:20:36 | 424,499 | 2020-05-02 15:48:42 | https://stackoverflow.com/q/8206280 | https://stackoverflow.com/a/8206295 | <p>This can be done with a <a href="http://www.grymoire.com/Unix/Sed.html#uh-30" rel="noreferrer">sed one-liner</a>:</p>
<pre><code>sed '/^#/d'
</code></pre>
<p>This says, "find all lines that start with # and delete them, leaving everything else."</p>
| <p>This can be done with a <a href="http://www.grymoire.com/Unix/Sed.html#uh-30" rel="noreferrer">sed one-liner</a>:</p> <pre><code>sed '/^#/d' </code></pre> <p>This says, "find all lines that start with # and delete them, leaving everything else."</p> | 387, 5282 | bash, sed | <h1>Delete all lines beginning with a # from a file</h1>
<p>All of the lines with comments in a file begin with <code>#</code>. How can I delete all of the lines (and only those lines) which begin with <code>#</code>? Other lines containing <code>#</code>, but not at the beginning of the line should be ignored.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 411 | bash | # Delete all lines beginning with a # from a file
All of the lines with comments in a file begin with `#`. How can I delete all of the lines (and only those lines) which begin with `#`? Other lines containing `#`, but not at the beginning of the line should be ignored. | This can be done with a [sed one-liner](http://www.grymoire.com/Unix/Sed.html#uh-30):
```
sed '/^#/d'
```
This says, "find all lines that start with # and delete them, leaving everything else." |
947810 | How to save a Python interactive session? | 479 | 2009-06-03 23:21:02 | <p>I find myself frequently using Python's interpreter to work with databases, files, etc -- basically a lot of manual formatting of semi-structured data. I don't properly save and clean up the useful bits as often as I would like. Is there a way to save my input into the shell (db connections, variable assignments, ... | 240,510 | 11,596 | 2025-11-20 17:30:40 | 947,846 | 461 | 2009-06-03 23:34:21 | 107,366 | 2017-06-20 18:14:30 | https://stackoverflow.com/q/947810 | https://stackoverflow.com/a/947846 | <p><a href="https://ipython.org/" rel="noreferrer">IPython</a> is extremely useful if you like using interactive sessions. For example for your use-case there is <a href="http://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-save" rel="noreferrer">the <code>%save</code> magic command</a>, you just input... | <p><a href="https://ipython.org/" rel="noreferrer">IPython</a> is extremely useful if you like using interactive sessions. For example for your use-case there is <a href="http://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-save" rel="noreferrer">the <code>%save</code> magic command</a>, you just input... | 16, 390, 63533, 68006 | interactive-session, python, read-eval-print-loop, shell | <h1>How to save a Python interactive session?</h1>
<p>I find myself frequently using Python's interpreter to work with databases, files, etc -- basically a lot of manual formatting of semi-structured data. I don't properly save and clean up the useful bits as often as I would like. Is there a way to save my input int... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 412 | bash | # How to save a Python interactive session?
I find myself frequently using Python's interpreter to work with databases, files, etc -- basically a lot of manual formatting of semi-structured data. I don't properly save and clean up the useful bits as often as I would like. Is there a way to save my input into the shell... | [IPython](https://ipython.org/) is extremely useful if you like using interactive sessions. For example for your use-case there is [the `%save` magic command](http://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-save), you just input `%save my_useful_session 10-20 23` to save input lines 10 to 20 and 2... |
947897 | Block Comments in a Shell Script | 361 | 2009-06-03 23:53:34 | <p>Is there a simple way to comment out a block of code in a shell script?</p>
| 485,940 | 75,694 | 2025-10-15 20:29:46 | 947,936 | 461 | 2009-06-04 00:02:58 | 93,279 | 2017-06-23 14:17:14 | https://stackoverflow.com/q/947897 | https://stackoverflow.com/a/947936 | <p>In bash:</p>
<pre><code>#!/bin/bash
echo before comment
: <<'END'
bla bla
blurfl
END
echo after comment
</code></pre>
<p>The <code>'</code> and <code>'</code> around the <code>END</code> delimiter are important, otherwise things inside the block like for example <code>$(command)</code> will be parsed and exe... | <p>In bash:</p> <pre><code>#!/bin/bash echo before comment : <<'END' bla bla blurfl END echo after comment </code></pre> <p>The <code>'</code> and <code>'</code> around the <code>END</code> delimiter are important, otherwise things inside the block like for example <code>$(command)</code> will be parsed and exe... | 390 | shell | <h1>Block Comments in a Shell Script</h1>
<p>Is there a simple way to comment out a block of code in a shell script?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 413 | bash | # Block Comments in a Shell Script
Is there a simple way to comment out a block of code in a shell script? | In bash:
```
#!/bin/bash
echo before comment
: <<'END'
bla bla
blurfl
END
echo after comment
```
The `'` and `'` around the `END` delimiter are important, otherwise things inside the block like for example `$(command)` will be parsed and executed.
For an explanation, see [this](https://stackoverflow.com/questions/32... |
9293887 | How to read a space-delimited string into an array in Bash? | 307 | 2012-02-15 12:59:07 | <p>I have a variable which contains a space-delimited string:</p>
<pre><code>line="1 1.50 string"
</code></pre>
<p>I want to split that string with space as a delimiter and store the result in an array, so that the following:</p>
<pre><code>echo ${arr[0]}
echo ${arr[1]}
echo ${arr[2]}
</code></pre>
<p>outputs</p>
... | 388,929 | 698,282 | 2023-08-29 10:48:20 | 9,294,015 | 461 | 2012-02-15 13:06:47 | 348,785 | 2022-02-19 00:07:20 | https://stackoverflow.com/q/9293887 | https://stackoverflow.com/a/9294015 | <p>In order to convert a string into an array, create an array from the string, letting the string get split naturally according to the <code>IFS</code> (Internal Field Separator) variable, which is the space char by default:</p>
<pre><code>arr=($line)
</code></pre>
<p>or pass the string to the stdin of the <code>read<... | <p>In order to convert a string into an array, create an array from the string, letting the string get split naturally according to the <code>IFS</code> (Internal Field Separator) variable, which is the space char by default:</p> <pre><code>arr=($line) </code></pre> <p>or pass the string to the stdin of the <code>read<... | 114, 139, 387, 390, 16168 | arrays, bash, delimiter, shell, string | <h1>How to read a space-delimited string into an array in Bash?</h1>
<p>I have a variable which contains a space-delimited string:</p>
<pre><code>line="1 1.50 string"
</code></pre>
<p>I want to split that string with space as a delimiter and store the result in an array, so that the following:</p>
<pre><code>echo ${... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 414 | bash | # How to read a space-delimited string into an array in Bash?
I have a variable which contains a space-delimited string:
```
line="1 1.50 string"
```
I want to split that string with space as a delimiter and store the result in an array, so that the following:
```
echo ${arr[0]}
echo ${arr[1]}
echo ${arr[2]}
```
o... | In order to convert a string into an array, create an array from the string, letting the string get split naturally according to the `IFS` (Internal Field Separator) variable, which is the space char by default:
```
arr=($line)
```
or pass the string to the stdin of the `read` command using the [herestring](https://s... |
9457233 | Unlimited Bash History | 515 | 2012-02-26 21:15:35 | <p>I want my <code>.bash_history</code> file to be unlimited. e.g. So I can always go back and see how I built/configured something, or what that nifty command was, or how some command broke something weeks ago. How do I change this setting?</p>
| 220,360 | 802,767 | 2024-05-14 07:22:59 | 12,234,989 | 460 | 2012-09-02 09:56:42 | 495,470 | 2021-07-27 04:08:03 | https://stackoverflow.com/q/9457233 | https://stackoverflow.com/a/12234989 | <p>Set <code>HISTSIZE</code> and <code>HISTFILESIZE</code> in .bashrc to an empty string:</p>
<pre><code>HISTSIZE=
HISTFILESIZE=
</code></pre>
<p>In <a href="http://tiswww.case.edu/php/chet/bash/CHANGES" rel="noreferrer">bash 4.3 and later</a> you can also use <code>HISTSIZE=-1 HISTFILESIZE=-1</code>:</p>
<pre class="... | <p>Set <code>HISTSIZE</code> and <code>HISTFILESIZE</code> in .bashrc to an empty string:</p> <pre><code>HISTSIZE= HISTFILESIZE= </code></pre> <p>In <a href="http://tiswww.case.edu/php/chet/bash/CHANGES" rel="noreferrer">bash 4.3 and later</a> you can also use <code>HISTSIZE=-1 HISTFILESIZE=-1</code>:</p> <pre class="... | 34, 387 | bash, unix | <h1>Unlimited Bash History</h1>
<p>I want my <code>.bash_history</code> file to be unlimited. e.g. So I can always go back and see how I built/configured something, or what that nifty command was, or how some command broke something weeks ago. How do I change this setting?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 415 | bash | # Unlimited Bash History
I want my `.bash_history` file to be unlimited. e.g. So I can always go back and see how I built/configured something, or what that nifty command was, or how some command broke something weeks ago. How do I change this setting? | Set `HISTSIZE` and `HISTFILESIZE` in .bashrc to an empty string:
```
HISTSIZE=
HISTFILESIZE=
```
In [bash 4.3 and later](http://tiswww.case.edu/php/chet/bash/CHANGES) you can also use `HISTSIZE=-1 HISTFILESIZE=-1`:
```
n. Setting HISTSIZE to a value less than zero causes the history list to be
unlimited (setti... |
10521061 | How to get an MD5 checksum in PowerShell | 261 | 2012-05-09 17:24:26 | <p>I would like to calculate an <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> checksum of some content. How do I do this in PowerShell?</p>
| 508,501 | 184,773 | 2024-04-30 22:52:42 | 10,521,162 | 460 | 2012-05-09 17:32:52 | 492,405 | 2022-01-14 07:41:27 | https://stackoverflow.com/q/10521061 | https://stackoverflow.com/a/10521162 | <p>Starting in PowerShell version 4, this is easy to do for files out of the box with the <a href="https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Get-FileHash?view=powershell-4.0" rel="noreferrer"><code>Get-FileHash</code></a> cmdlet:</p>
<pre><code>Get-FileHash <filepath> -Algor... | <p>Starting in PowerShell version 4, this is easy to do for files out of the box with the <a href="https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Get-FileHash?view=powershell-4.0" rel="noreferrer"><code>Get-FileHash</code></a> cmdlet:</p> <pre><code>Get-FileHash <filepath> -Algor... | 526, 24067 | powershell, powershell-2.0 | <h1>How to get an MD5 checksum in PowerShell</h1>
<p>I would like to calculate an <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> checksum of some content. How do I do this in PowerShell?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 416 | bash | # How to get an MD5 checksum in PowerShell
I would like to calculate an [MD5](http://en.wikipedia.org/wiki/MD5) checksum of some content. How do I do this in PowerShell? | Starting in PowerShell version 4, this is easy to do for files out of the box with the [`Get-FileHash`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Get-FileHash?view=powershell-4.0) cmdlet:
```
Get-FileHash <filepath> -Algorithm MD5
```
This is certainly preferable since it avoids... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.