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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
4366533 | How to remove the lines which appear on file B from another file A? | 211 | 2010-12-06 12:48:14 | <p>I have a large <strong>file A</strong> (consisting of emails), one line for each mail. I also have another <strong>file B</strong> that contains another set of mails.</p>
<p>Which command would I use to remove all the addresses that appear in file B from the file A.</p>
<p>So, if file A contained:</p>
<pre><code>... | 127,486 | 435,093 | 2023-01-08 00:20:34 | 4,366,568 | 249 | 2010-12-06 12:53:24 | 21,755 | 2019-07-20 13:32:48 | https://stackoverflow.com/q/4366533 | https://stackoverflow.com/a/4366568 | <p>If the files are sorted (they are in your example):</p>
<pre><code>comm -23 file1 file2
</code></pre>
<p><code>-23</code> suppresses the lines that are in both files, or only in file 2. If the files are not sorted, pipe them through <code>sort</code> first...</p>
<p>See the <a href="https://linux.die.net/man/1/co... | <p>If the files are sorted (they are in your example):</p> <pre><code>comm -23 file1 file2 </code></pre> <p><code>-23</code> suppresses the lines that are in both files, or only in file 2. If the files are not sorted, pipe them through <code>sort</code> first...</p> <p>See the <a href="https://linux.die.net/man/1/co... | 58, 390, 606, 1271, 5282 | diff, grep, linux, sed, shell | <h1>How to remove the lines which appear on file B from another file A?</h1>
<p>I have a large <strong>file A</strong> (consisting of emails), one line for each mail. I also have another <strong>file B</strong> that contains another set of mails.</p>
<p>Which command would I use to remove all the addresses that appear... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 821 | bash | # How to remove the lines which appear on file B from another file A?
I have a large **file A** (consisting of emails), one line for each mail. I also have another **file B** that contains another set of mails.
Which command would I use to remove all the addresses that appear in file B from the file A.
So, if file A... | If the files are sorted (they are in your example):
```
comm -23 file1 file2
```
`-23` suppresses the lines that are in both files, or only in file 2. If the files are not sorted, pipe them through `sort` first...
See the [man page here](https://linux.die.net/man/1/comm) |
5998066 | Bash script - variable content as a command to run | 195 | 2011-05-13 21:40:00 | <p>I have a Perl script that gives me a defined list of random numbers that correspond to the lines of a file. Next I want to extract those lines from the file using <code>sed</code>.</p>
<pre><code>#!/bin/bash
count=$(cat last_queries.txt | wc -l)
var=$(perl test.pl test2 $count)
</code></pre>
<p>The variable <code>va... | 331,284 | 713,560 | 2023-10-06 08:54:41 | 6,002,329 | 249 | 2011-05-14 14:07:53 | 531,201 | 2020-03-04 20:03:44 | https://stackoverflow.com/q/5998066 | https://stackoverflow.com/a/6002329 | <p>You just need to do:</p>
<pre><code>#!/bin/bash
count=$(cat last_queries.txt | wc -l)
$(perl test.pl test2 $count)
</code></pre>
<p>However, if you want to call your Perl command later, and that's why you want to assign it to a variable, then:</p>
<pre><code>#!/bin/bash
count=$(cat last_queries.txt | wc -l)
var="... | <p>You just need to do:</p> <pre><code>#!/bin/bash count=$(cat last_queries.txt | wc -l) $(perl test.pl test2 $count) </code></pre> <p>However, if you want to call your Perl command later, and that's why you want to assign it to a variable, then:</p> <pre><code>#!/bin/bash count=$(cat last_queries.txt | wc -l) var="... | 387 | bash | <h1>Bash script - variable content as a command to run</h1>
<p>I have a Perl script that gives me a defined list of random numbers that correspond to the lines of a file. Next I want to extract those lines from the file using <code>sed</code>.</p>
<pre><code>#!/bin/bash
count=$(cat last_queries.txt | wc -l)
var=$(perl ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 822 | bash | # Bash script - variable content as a command to run
I have a Perl script that gives me a defined list of random numbers that correspond to the lines of a file. Next I want to extract those lines from the file using `sed`.
```
#!/bin/bash
count=$(cat last_queries.txt | wc -l)
var=$(perl test.pl test2 $count)
```
The... | You just need to do:
```
#!/bin/bash
count=$(cat last_queries.txt | wc -l)
$(perl test.pl test2 $count)
```
However, if you want to call your Perl command later, and that's why you want to assign it to a variable, then:
```
#!/bin/bash
count=$(cat last_queries.txt | wc -l)
var="perl test.pl test2 $count" # You need ... |
20638520 | Appropriate hashbang for Node.js scripts | 147 | 2013-12-17 15:45:02 | <p>I'm trying to create a script for node.js that will work in multiple environments. Particularly for me, I'm switching back and forth between OS X and Ubuntu. In the former, Node is installed as <code>node</code>, but in the latter it is <code>nodejs</code>. At the top of my script, I can have:</p>
<pre><code>#!/... | 55,656 | 454,533 | 2018-03-07 04:51:08 | 28,646,724 | 249 | 2015-02-21 14:09:22 | 1,709,587 | 2015-02-21 15:59:50 | https://stackoverflow.com/q/20638520 | https://stackoverflow.com/a/28646724 | <p>If your script is intended for use by Node developers, you should absolutely just use</p>
<pre><code>#!/usr/bin/env node
</code></pre>
<p>and not bother trying for compatibility with people who only have Node installed as <code>nodejs</code>.</p>
<p>Rationale:</p>
<ul>
<li>It's what the cool kids are doing, and ... | <p>If your script is intended for use by Node developers, you should absolutely just use</p> <pre><code>#!/usr/bin/env node </code></pre> <p>and not bother trying for compatibility with people who only have Node installed as <code>nodejs</code>.</p> <p>Rationale:</p> <ul> <li>It's what the cool kids are doing, and ... | 390, 8780, 46426 | node.js, shebang, shell | <h1>Appropriate hashbang for Node.js scripts</h1>
<p>I'm trying to create a script for node.js that will work in multiple environments. Particularly for me, I'm switching back and forth between OS X and Ubuntu. In the former, Node is installed as <code>node</code>, but in the latter it is <code>nodejs</code>. At the... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 823 | bash | # Appropriate hashbang for Node.js scripts
I'm trying to create a script for node.js that will work in multiple environments. Particularly for me, I'm switching back and forth between OS X and Ubuntu. In the former, Node is installed as `node`, but in the latter it is `nodejs`. At the top of my script, I can have:
``... | If your script is intended for use by Node developers, you should absolutely just use
```
#!/usr/bin/env node
```
and not bother trying for compatibility with people who only have Node installed as `nodejs`.
Rationale:
- It's what the cool kids are doing, and if you don't do it too, you're not cool. Major node proj... |
4060880 | Passing argument to alias in bash | 140 | 2010-10-30 22:03:49 | <p>Is it possible to do the following:</p>
<p>I want to run the following:</p>
<pre><code>mongodb bin/mongod
</code></pre>
<p>In my bash_profile I have</p>
<pre><code>alias = "./path/to/mongodb/$1"
</code></pre>
| 122,221 | 238,975 | 2017-04-11 10:19:58 | 4,060,984 | 249 | 2010-10-30 22:37:41 | 26,428 | 2016-07-13 15:35:29 | https://stackoverflow.com/q/4060880 | https://stackoverflow.com/a/4060984 | <p>An alias will expand to the string it represents. Anything after the alias will appear after its expansion without needing to be or able to be passed as explicit arguments (e.g. <code>$1</code>).</p>
<pre><code>$ alias foo='/path/to/bar'
$ foo some args
</code></pre>
<p>will get expanded to</p>
<pre><code>$ /path... | <p>An alias will expand to the string it represents. Anything after the alias will appear after its expansion without needing to be or able to be passed as explicit arguments (e.g. <code>$1</code>).</p> <pre><code>$ alias foo='/path/to/bar' $ foo some args </code></pre> <p>will get expanded to</p> <pre><code>$ /path... | 387, 390 | bash, shell | <h1>Passing argument to alias in bash</h1>
<p>Is it possible to do the following:</p>
<p>I want to run the following:</p>
<pre><code>mongodb bin/mongod
</code></pre>
<p>In my bash_profile I have</p>
<pre><code>alias = "./path/to/mongodb/$1"
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 824 | bash | # Passing argument to alias in bash
Is it possible to do the following:
I want to run the following:
```
mongodb bin/mongod
```
In my bash_profile I have
```
alias = "./path/to/mongodb/$1"
``` | An alias will expand to the string it represents. Anything after the alias will appear after its expansion without needing to be or able to be passed as explicit arguments (e.g. `$1`).
```
$ alias foo='/path/to/bar'
$ foo some args
```
will get expanded to
```
$ /path/to/bar some args
```
If you want to use explici... |
1848415 | Remove a character from the end of a variable | 139 | 2009-12-04 17:20:10 | <p>Bash auto completion appends a / at the end of a directory name. How I can strip this off from a positional parameter?</p>
<pre><code>#!/bin/sh
target=$1
function backup(){
date=`date "+%y%m%d_%H%M%S"`
PWD=`pwd`
path=$PWD/$target
tar czf /tmp/$date$target.tar.gz $path
}
backup
</code></pre>
| 72,044 | 223,703 | 2024-07-02 15:08:20 | 1,848,456 | 249 | 2009-12-04 17:27:42 | 183,172 | 2024-07-02 15:08:20 | https://stackoverflow.com/q/1848415 | https://stackoverflow.com/a/1848456 | <p>Use</p>
<pre><code>target=${1%/}
</code></pre>
<p>A <a href="http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion" rel="nofollow noreferrer">reference</a>.</p>
| <p>Use</p> <pre><code>target=${1%/} </code></pre> <p>A <a href="http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion" rel="nofollow noreferrer">reference</a>.</p> | 387, 390 | bash, shell | <h1>Remove a character from the end of a variable</h1>
<p>Bash auto completion appends a / at the end of a directory name. How I can strip this off from a positional parameter?</p>
<pre><code>#!/bin/sh
target=$1
function backup(){
date=`date "+%y%m%d_%H%M%S"`
PWD=`pwd`
path=$PWD/$target
tar czf /tmp... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 825 | bash | # Remove a character from the end of a variable
Bash auto completion appends a / at the end of a directory name. How I can strip this off from a positional parameter?
```
#!/bin/sh
target=$1
function backup(){
date=`date "+%y%m%d_%H%M%S"`
PWD=`pwd`
path=$PWD/$target
tar czf /tmp/$date$target.tar.gz $path
}
... | Use
```
target=${1%/}
```
A [reference](http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion). |
18093871 | How can I do division with variables in a Linux shell? | 179 | 2013-08-07 02:56:17 | <p>When I run commands in my shell as below, it returns an <code>expr: non-integer argument</code> error. Can someone please explain this to me?</p>
<pre><code>$ x=20
$ y=5
$ expr x / y
expr: non-integer argument
</code></pre>
| 442,191 | 1,464,078 | 2020-11-08 19:55:37 | 18,093,887 | 248 | 2013-08-07 02:59:04 | 1,553,090 | 2013-08-07 03:04:26 | https://stackoverflow.com/q/18093871 | https://stackoverflow.com/a/18093887 | <p>Those variables are shell variables. To expand them as parameters to another program (<em>ie</em> <code>expr</code>), you need to use the <code>$</code> prefix:</p>
<pre><code>expr $x / $y
</code></pre>
<p>The reason it complained is because it thought you were trying to operate on alphabetic characters (<em>ie</... | <p>Those variables are shell variables. To expand them as parameters to another program (<em>ie</em> <code>expr</code>), you need to use the <code>$</code> prefix:</p> <pre><code>expr $x / $y </code></pre> <p>The reason it complained is because it thought you were trying to operate on alphabetic characters (<em>ie</... | 58, 276, 387, 390, 10776 | bash, division, linux, shell, variables | <h1>How can I do division with variables in a Linux shell?</h1>
<p>When I run commands in my shell as below, it returns an <code>expr: non-integer argument</code> error. Can someone please explain this to me?</p>
<pre><code>$ x=20
$ y=5
$ expr x / y
expr: non-integer argument
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 826 | bash | # How can I do division with variables in a Linux shell?
When I run commands in my shell as below, it returns an `expr: non-integer argument` error. Can someone please explain this to me?
```
$ x=20
$ y=5
$ expr x / y
expr: non-integer argument
``` | Those variables are shell variables. To expand them as parameters to another program (*ie* `expr`), you need to use the `$` prefix:
```
expr $x / $y
```
The reason it complained is because it thought you were trying to operate on alphabetic characters (*ie* non-integer)
If you are using the Bash shell, you can achie... |
29937568 | How can I find the product GUID of an installed MSI setup? | 112 | 2015-04-29 07:54:30 | <p>I need to find the <strong>product GUID</strong> for an <strong>installed MSI file</strong> in order to perform maintenance such as <strong><code>patching</code></strong>, <strong><code>uninstall</code></strong> (<a href="https://stackoverflow.com/questions/450027/uninstalling-an-msi-file-from-the-command-line-witho... | 615,901 | 129,130 | 2023-03-03 16:46:34 | 29,937,569 | 248 | 2015-04-29 07:54:30 | 129,130 | 2022-04-15 13:03:39 | https://stackoverflow.com/q/29937568 | https://stackoverflow.com/a/29937569 | <blockquote>
<p>For <strong>upgrade code</strong> retrieval:
<strong><a href="https://stackoverflow.com/questions/46637094/how-can-i-find-the-upgrade-code-for-an-installed-msi-file/46637095#46637095">How can I find the Upgrade Code for an installed MSI file?</a></strong> (<a href="https://github.com/glytzhkof/all/blob/... | <blockquote> <p>For <strong>upgrade code</strong> retrieval: <strong><a href="https://stackoverflow.com/questions/46637094/how-can-i-find-the-upgrade-code-for-an-installed-msi-file/46637095#46637095">How can I find the Upgrade Code for an installed MSI file?</a></strong> (<a href="https://github.com/glytzhkof/all/blob/... | 526, 529, 1914, 4520, 14038 | installation, powershell, uninstallation, windows-installer, wix | <h1>How can I find the product GUID of an installed MSI setup?</h1>
<p>I need to find the <strong>product GUID</strong> for an <strong>installed MSI file</strong> in order to perform maintenance such as <strong><code>patching</code></strong>, <strong><code>uninstall</code></strong> (<a href="https://stackoverflow.com/q... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 827 | bash | # How can I find the product GUID of an installed MSI setup?
I need to find the **product GUID** for an **installed MSI file** in order to perform maintenance such as **`patching`**, **`uninstall`** ([**how-to uninstall**](https://stackoverflow.com/questions/450027/uninstalling-an-msi-file-from-the-command-line-withou... | > For **upgrade code** retrieval:
> **[How can I find the Upgrade Code for an installed MSI file?](https://stackoverflow.com/questions/46637094/how-can-i-find-the-upgrade-code-for-an-installed-msi-file/46637095#46637095)** ([or use the html table export script](https://github.com/glytzhkof/all/blob/master/MsiHtmlReport... |
360201 | How do I kill background processes / jobs when my shell script exits? | 299 | 2008-12-11 17:33:00 | <p>I am looking for a way to clean up the mess when my top-level script exits.</p>
<p>Especially if I want to use <code>set -e</code>, I wish the background process would die when the script exits.</p>
| 188,766 | 1,277,510 | 2024-07-02 10:01:15 | 360,275 | 247 | 2008-12-11 17:57:33 | 34,509 | 2008-12-12 16:33:04 | https://stackoverflow.com/q/360201 | https://stackoverflow.com/a/360275 | <p>To clean up some mess, <code>trap</code> can be used. It can provide a list of stuff executed when a specific signal arrives:</p>
<pre><code>trap "echo hello" SIGINT
</code></pre>
<p>but can also be used to execute something if the shell exits:</p>
<pre><code>trap "killall background" EXIT
</code></pre>
<p>It's ... | <p>To clean up some mess, <code>trap</code> can be used. It can provide a list of stuff executed when a specific signal arrives:</p> <pre><code>trap "echo hello" SIGINT </code></pre> <p>but can also be used to execute something if the shell exits:</p> <pre><code>trap "killall background" EXIT </code></pre> <p>It's ... | 390 | shell | <h1>How do I kill background processes / jobs when my shell script exits?</h1>
<p>I am looking for a way to clean up the mess when my top-level script exits.</p>
<p>Especially if I want to use <code>set -e</code>, I wish the background process would die when the script exits.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 828 | bash | # How do I kill background processes / jobs when my shell script exits?
I am looking for a way to clean up the mess when my top-level script exits.
Especially if I want to use `set -e`, I wish the background process would die when the script exits. | To clean up some mess, `trap` can be used. It can provide a list of stuff executed when a specific signal arrives:
```
trap "echo hello" SIGINT
```
but can also be used to execute something if the shell exits:
```
trap "killall background" EXIT
```
It's a builtin, so `help trap` will give you information (works wit... |
37104273 | How to set aliases in Git Bash for Windows? | 259 | 2016-05-08 20:04:29 | <p>How to alias commands in Git Bash for Windows downloaded from <a href="https://git-scm.com/" rel="noreferrer">git-scm</a>?</p>
<p>I mean Bash commands, not Git.</p>
<p>I’m on Windows 7.</p>
<hr />
<p>Edit:</p>
<p>Writing aliases in <code>.bashrc</code> file (as suggested by <a href="https://stackoverflow.com/a/37104... | 234,187 | 5,826,730 | 2025-08-13 15:21:32 | 37,104,593 | 247 | 2016-05-08 20:40:16 | 1,796,345 | 2019-02-15 10:12:31 | https://stackoverflow.com/q/37104273 | https://stackoverflow.com/a/37104593 | <p>To configure bash aliases, it's the same as if you were on a Unix platform: put them in a <code>.bashrc</code> in your home:</p>
<pre><code>cd
echo alias ll=\'ls -l\' >> .bashrc
</code></pre>
<p>To have this change taken into account you should then either source this file (ie: run <code>source .bashrc</code... | <p>To configure bash aliases, it's the same as if you were on a Unix platform: put them in a <code>.bashrc</code> in your home:</p> <pre><code>cd echo alias ll=\'ls -l\' >> .bashrc </code></pre> <p>To have this change taken into account you should then either source this file (ie: run <code>source .bashrc</code... | 387, 61874 | bash, git-bash | <h1>How to set aliases in Git Bash for Windows?</h1>
<p>How to alias commands in Git Bash for Windows downloaded from <a href="https://git-scm.com/" rel="noreferrer">git-scm</a>?</p>
<p>I mean Bash commands, not Git.</p>
<p>I’m on Windows 7.</p>
<hr />
<p>Edit:</p>
<p>Writing aliases in <code>.bashrc</code> file (as su... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 829 | bash | # How to set aliases in Git Bash for Windows?
How to alias commands in Git Bash for Windows downloaded from [git-scm](https://git-scm.com/)?
I mean Bash commands, not Git.
I’m on Windows 7.
---
Edit:
Writing aliases in `.bashrc` file (as suggested by [@gturri](https://stackoverflow.com/a/37104593/9449426)) not ad... | To configure bash aliases, it's the same as if you were on a Unix platform: put them in a `.bashrc` in your home:
```
cd
echo alias ll=\'ls -l\' >> .bashrc
```
To have this change taken into account you should then either source this file (ie: run `source .bashrc`) or restart your terminal
(In some cases* you can fi... |
11376899 | How do I navigate my Windows folders like C drive in MSYS? | 156 | 2012-07-07 16:50:37 | <p>How do I navigate through my Windows folder structure within the MSYS shell? When I start the shell, all I see is a '~'. I type 'ls' and the folder is empty. I just want to know how to get to my c drive.</p>
| 118,363 | 1,024,973 | 2023-09-21 19:32:50 | 11,376,918 | 247 | 2012-07-07 16:54:19 | 1,461,163 | 2016-07-16 23:25:05 | https://stackoverflow.com/q/11376899 | https://stackoverflow.com/a/11376918 | <p><code>cd /c/</code> to access C:</p>
<p><code>cd /d/</code> for D:</p>
<p>etc.</p>
| <p><code>cd /c/</code> to access C:</p> <p><code>cd /d/</code> for D:</p> <p>etc.</p> | 387, 13232 | bash, msys | <h1>How do I navigate my Windows folders like C drive in MSYS?</h1>
<p>How do I navigate through my Windows folder structure within the MSYS shell? When I start the shell, all I see is a '~'. I type 'ls' and the folder is empty. I just want to know how to get to my c drive.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 830 | bash | # How do I navigate my Windows folders like C drive in MSYS?
How do I navigate through my Windows folder structure within the MSYS shell? When I start the shell, all I see is a '~'. I type 'ls' and the folder is empty. I just want to know how to get to my c drive. | `cd /c/` to access C:
`cd /d/` for D:
etc. |
2425870 | Multithreading in Bash | 122 | 2010-03-11 14:48:09 | <p>I would like to introduce multithreading feature in my shell script.</p>
<p>I have a script which calls the function <code>read_cfg()</code> with different arguments.
Each of these function calls are independent.</p>
<p>Would it be possible to instantiate these function calls (not scripts) parallelly.
Please let m... | 210,935 | 276,859 | 2016-09-21 22:41:27 | 2,425,914 | 247 | 2010-03-11 14:52:49 | 119,159 | 2010-03-11 15:01:24 | https://stackoverflow.com/q/2425870 | https://stackoverflow.com/a/2425914 | <p>Sure, just add <code>&</code> after the command:</p>
<pre><code>read_cfg cfgA &
read_cfg cfgB &
read_cfg cfgC &
wait
</code></pre>
<p>all those jobs will then run in the background simultaneously. The optional <code>wait</code> command will then wait for all the jobs to finish.</p>
<p>Each command... | <p>Sure, just add <code>&</code> after the command:</p> <pre><code>read_cfg cfgA & read_cfg cfgB & read_cfg cfgC & wait </code></pre> <p>all those jobs will then run in the background simultaneously. The optional <code>wait</code> command will then wait for all the jobs to finish.</p> <p>Each command... | 58, 387, 390, 1381 | bash, linux, multithreading, shell | <h1>Multithreading in Bash</h1>
<p>I would like to introduce multithreading feature in my shell script.</p>
<p>I have a script which calls the function <code>read_cfg()</code> with different arguments.
Each of these function calls are independent.</p>
<p>Would it be possible to instantiate these function calls (not s... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 831 | bash | # Multithreading in Bash
I would like to introduce multithreading feature in my shell script.
I have a script which calls the function `read_cfg()` with different arguments.
Each of these function calls are independent.
Would it be possible to instantiate these function calls (not scripts) parallelly.
Please let me ... | Sure, just add `&` after the command:
```
read_cfg cfgA &
read_cfg cfgB &
read_cfg cfgC &
wait
```
all those jobs will then run in the background simultaneously. The optional `wait` command will then wait for all the jobs to finish.
Each command will run in a separate process, so it's technically not "multithreading... |
2038181 | How to output something in PowerShell | 265 | 2010-01-10 19:08:08 | <p>I am running a PowerShell script from within a batch file. The script fetches a web page and checks whether the page's content is the string "OK".</p>
<p>The PowerShell script returns an error level to the batch script.</p>
<p>The batch script is executed by <a href="http://www.scriptftp.com" rel="noreferrer">Scri... | 849,129 | 187,606 | 2025-01-10 11:19:07 | 2,042,981 | 246 | 2010-01-11 16:01:47 | 243 | 2014-12-29 11:35:50 | https://stackoverflow.com/q/2038181 | https://stackoverflow.com/a/2042981 | <p>Simply outputting something is PowerShell is a thing of beauty - and one its greatest strengths. For example, the common Hello, World! application is reduced to a single line:</p>
<pre><code>"Hello, World!"
</code></pre>
<p>It creates a string object, assigns the aforementioned value, and being the last item on th... | <p>Simply outputting something is PowerShell is a thing of beauty - and one its greatest strengths. For example, the common Hello, World! application is reduced to a single line:</p> <pre><code>"Hello, World!" </code></pre> <p>It creates a string object, assigns the aforementioned value, and being the last item on th... | 64, 526 | powershell, windows | <h1>How to output something in PowerShell</h1>
<p>I am running a PowerShell script from within a batch file. The script fetches a web page and checks whether the page's content is the string "OK".</p>
<p>The PowerShell script returns an error level to the batch script.</p>
<p>The batch script is executed by <a href="... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 832 | bash | # How to output something in PowerShell
I am running a PowerShell script from within a batch file. The script fetches a web page and checks whether the page's content is the string "OK".
The PowerShell script returns an error level to the batch script.
The batch script is executed by [ScriptFTP](http://www.scriptftp... | Simply outputting something is PowerShell is a thing of beauty - and one its greatest strengths. For example, the common Hello, World! application is reduced to a single line:
```
"Hello, World!"
```
It creates a string object, assigns the aforementioned value, and being the last item on the command pipeline it calls... |
845863 | How to use > in an xargs command? | 200 | 2009-05-10 18:51:37 | <p>I want to find a bash command that will let me grep every file in a directory and write the output of that grep to a separate file. My guess would have been to do something like this</p>
<pre><code>ls -1 | xargs -I{} "grep ABC '{}' > '{}'.out"
</code></pre>
<p>but, as far as I know, xargs doesn't like the doub... | 83,309 | 365,377 | 2021-12-05 00:08:48 | 845,928 | 246 | 2009-05-10 19:22:38 | 58,803 | 2017-06-30 12:32:54 | https://stackoverflow.com/q/845863 | https://stackoverflow.com/a/845928 | <p>Do not make the mistake of doing this:</p>
<pre><code>sh -c "grep ABC {} > {}.out"
</code></pre>
<p>This will break under a lot of conditions, including funky filenames and is impossible to quote right. Your <code>{}</code> must always be a single completely separate argument to the command to avoid code inject... | <p>Do not make the mistake of doing this:</p> <pre><code>sh -c "grep ABC {} > {}.out" </code></pre> <p>This will break under a lot of conditions, including funky filenames and is impossible to quote right. Your <code>{}</code> must always be a single completely separate argument to the command to avoid code inject... | 387, 10326, 31901 | bash, http-redirect, xargs | <h1>How to use > in an xargs command?</h1>
<p>I want to find a bash command that will let me grep every file in a directory and write the output of that grep to a separate file. My guess would have been to do something like this</p>
<pre><code>ls -1 | xargs -I{} "grep ABC '{}' > '{}'.out"
</code></pre>
<p>but, as... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 833 | bash | # How to use > in an xargs command?
I want to find a bash command that will let me grep every file in a directory and write the output of that grep to a separate file. My guess would have been to do something like this
```
ls -1 | xargs -I{} "grep ABC '{}' > '{}'.out"
```
but, as far as I know, xargs doesn't like th... | Do not make the mistake of doing this:
```
sh -c "grep ABC {} > {}.out"
```
This will break under a lot of conditions, including funky filenames and is impossible to quote right. Your `{}` must always be a single completely separate argument to the command to avoid code injection bugs. What you need to do, is this:
... |
16918602 | How to base64 encode image in linux bash / shell | 183 | 2013-06-04 13:03:22 | <p>I'm trying to base64 encode an image in a shell script and put it into variable:</p>
<pre><code>test="$(printf DSC_0251.JPG | base64)"
echo $test
RFNDXzAyNTEuSlBH
</code></pre>
<p>I've also tried something like this:</p>
<pre><code>test=\`echo -ne DSC_0251.JPG | base64\`
</code></pre>
<p>but still with no succes... | 291,002 | 2,139,753 | 2025-09-03 01:06:05 | 16,918,741 | 246 | 2013-06-04 13:09:52 | 1,126,841 | 2013-06-04 13:09:52 | https://stackoverflow.com/q/16918602 | https://stackoverflow.com/a/16918741 | <p>You need to use <code>cat</code> to get the <em>contents</em> of the file named 'DSC_0251.JPG', rather than the filename itself.</p>
<pre><code>test="$(cat DSC_0251.JPG | base64)"
</code></pre>
<p>However, <code>base64</code> can read from the file itself:</p>
<pre><code>test=$( base64 DSC_0251.JPG )
</code></pre... | <p>You need to use <code>cat</code> to get the <em>contents</em> of the file named 'DSC_0251.JPG', rather than the filename itself.</p> <pre><code>test="$(cat DSC_0251.JPG | base64)" </code></pre> <p>However, <code>base64</code> can read from the file itself:</p> <pre><code>test=$( base64 DSC_0251.JPG ) </code></pre... | 58, 276, 390, 1011, 4985 | base64, image, linux, shell, variables | <h1>How to base64 encode image in linux bash / shell</h1>
<p>I'm trying to base64 encode an image in a shell script and put it into variable:</p>
<pre><code>test="$(printf DSC_0251.JPG | base64)"
echo $test
RFNDXzAyNTEuSlBH
</code></pre>
<p>I've also tried something like this:</p>
<pre><code>test=\`echo -ne DSC_0251... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 834 | bash | # How to base64 encode image in linux bash / shell
I'm trying to base64 encode an image in a shell script and put it into variable:
```
test="$(printf DSC_0251.JPG | base64)"
echo $test
RFNDXzAyNTEuSlBH
```
I've also tried something like this:
```
test=\`echo -ne DSC_0251.JPG | base64\`
```
but still with no succe... | You need to use `cat` to get the *contents* of the file named 'DSC_0251.JPG', rather than the filename itself.
```
test="$(cat DSC_0251.JPG | base64)"
```
However, `base64` can read from the file itself:
```
test=$( base64 DSC_0251.JPG )
``` |
10623907 | Null coalescing in powershell | 179 | 2012-05-16 17:49:16 | <p>Is there a null coalescing operator in powershell? </p>
<p>I'd like to be able to do these c# commands in powershell:</p>
<pre><code>var s = myval ?? "new value";
var x = myval == null ? "" : otherval;
</code></pre>
| 94,793 | 17,744 | 2023-10-30 11:28:49 | 10,642,435 | 246 | 2012-05-17 19:40:39 | 1,129,543 | 2020-03-27 15:45:19 | https://stackoverflow.com/q/10623907 | https://stackoverflow.com/a/10642435 | <h1>Powershell 7+</h1>
<p><a href="https://learn.microsoft.com/en-gb/powershell/scripting/whats-new/what-s-new-in-powershell-70?view=powershell-7#null-coalescing-assignment-and-conditional-operators" rel="noreferrer">Powershell 7 introduces</a> native null coalescing, null conditional assignment, and ternary operators... | <h1>Powershell 7+</h1> <p><a href="https://learn.microsoft.com/en-gb/powershell/scripting/whats-new/what-s-new-in-powershell-70?view=powershell-7#null-coalescing-assignment-and-conditional-operators" rel="noreferrer">Powershell 7 introduces</a> native null coalescing, null conditional assignment, and ternary operators... | 526, 18930 | null-coalescing-operator, powershell | <h1>Null coalescing in powershell</h1>
<p>Is there a null coalescing operator in powershell? </p>
<p>I'd like to be able to do these c# commands in powershell:</p>
<pre><code>var s = myval ?? "new value";
var x = myval == null ? "" : otherval;
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 835 | bash | # Null coalescing in powershell
Is there a null coalescing operator in powershell?
I'd like to be able to do these c# commands in powershell:
```
var s = myval ?? "new value";
var x = myval == null ? "" : otherval;
``` | # Powershell 7+
[Powershell 7 introduces](https://learn.microsoft.com/en-gb/powershell/scripting/whats-new/what-s-new-in-powershell-70?view=powershell-7#null-coalescing-assignment-and-conditional-operators) native null coalescing, null conditional assignment, and ternary operators in Powershell.
**Null Coalescing**
... |
2789319 | File content into unix variable with newlines | 172 | 2010-05-07 14:33:29 | <p>I have a text file test.txt with the following content:</p>
<pre><code>text1
text2
</code></pre>
<p>And I want to assign the content of the file to a UNIX variable, but when I do this: </p>
<pre><code>testvar=$(cat test.txt)
echo $testvar
</code></pre>
<p>the result is: </p>
<pre><code>text1 text2
</code></pre... | 161,275 | 335,523 | 2021-01-12 23:08:42 | 2,789,399 | 246 | 2010-05-07 14:44:33 | 14,860 | 2021-01-12 23:08:42 | https://stackoverflow.com/q/2789319 | https://stackoverflow.com/a/2789399 | <p>The assignment does <em>not</em> remove the newline characters, it's actually the <code>echo</code> doing this. You need simply put quotes around the string to maintain those newlines:</p>
<pre><code>echo "$testvar"
</code></pre>
<p>This will give the result you want. See the following transcript for a dem... | <p>The assignment does <em>not</em> remove the newline characters, it's actually the <code>echo</code> doing this. You need simply put quotes around the string to maintain those newlines:</p> <pre><code>echo "$testvar" </code></pre> <p>This will give the result you want. See the following transcript for a dem... | 34, 387 | bash, unix | <h1>File content into unix variable with newlines</h1>
<p>I have a text file test.txt with the following content:</p>
<pre><code>text1
text2
</code></pre>
<p>And I want to assign the content of the file to a UNIX variable, but when I do this: </p>
<pre><code>testvar=$(cat test.txt)
echo $testvar
</code></pre>
<p>t... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 836 | bash | # File content into unix variable with newlines
I have a text file test.txt with the following content:
```
text1
text2
```
And I want to assign the content of the file to a UNIX variable, but when I do this:
```
testvar=$(cat test.txt)
echo $testvar
```
the result is:
```
text1 text2
```
instead of
```
text1
t... | The assignment does *not* remove the newline characters, it's actually the `echo` doing this. You need simply put quotes around the string to maintain those newlines:
```
echo "$testvar"
```
This will give the result you want. See the following transcript for a demo:
```
pax> cat num1.txt ; x=$(cat num1.txt)
line 1
... |
31255699 | Double parenthesis with and without dollar | 121 | 2015-07-06 21:10:44 | <p>Is <code>$(...)</code> the same as <code>(...)</code> in Bash?</p>
<p>Also, is <code>$((...))</code> the same as <code>((...))</code>?</p>
<p>Also, is <code>${...}</code> the same as <code>{...}</code>?</p>
<p>More generally what does the dollar sign stand for? Thank you.</p>
| 31,986 | 4,732,511 | 2022-06-23 13:20:59 | 31,255,942 | 246 | 2015-07-06 21:27:21 | 3,030,305 | 2022-06-23 13:20:59 | https://stackoverflow.com/q/31255699 | https://stackoverflow.com/a/31255942 | <p>In the following, I use "returns" to indicate return values and "produces" to indicate "substitutes the resulting text."</p>
<ul>
<li><p><a href="https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html" rel="noreferrer"><code>$(...)</code></a> means execute the comm... | <p>In the following, I use "returns" to indicate return values and "produces" to indicate "substitutes the resulting text."</p> <ul> <li><p><a href="https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html" rel="noreferrer"><code>$(...)</code></a> means execute the comm... | 387 | bash | <h1>Double parenthesis with and without dollar</h1>
<p>Is <code>$(...)</code> the same as <code>(...)</code> in Bash?</p>
<p>Also, is <code>$((...))</code> the same as <code>((...))</code>?</p>
<p>Also, is <code>${...}</code> the same as <code>{...}</code>?</p>
<p>More generally what does the dollar sign stand for? Tha... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 837 | bash | # Double parenthesis with and without dollar
Is `$(...)` the same as `(...)` in Bash?
Also, is `$((...))` the same as `((...))`?
Also, is `${...}` the same as `{...}`?
More generally what does the dollar sign stand for? Thank you. | In the following, I use "returns" to indicate return values and "produces" to indicate "substitutes the resulting text."
- [`$(...)`](https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html) means execute the command in the parens in a subshell and produces its stdout. Example:
```
$ echo "... |
19733437 | Getting "command not found" error while comparing two strings in Bash | 113 | 2013-11-01 18:53:16 | <p>My whole script is currently this:</p>
<pre><code>#!/bin/sh
clear;
blanko="";
# Dummy-Variablen
variable=Testvariable;
if [[$variable == $blanko]];
then
echo "Nichts da!"
else
echo $variable
fi
</code></pre>
<p>and if I enter</p>
<pre><code>TestSelect.sh
</code></pre>
... | 145,359 | 2,946,018 | 2022-08-03 08:16:55 | 19,733,465 | 246 | 2013-11-01 18:55:05 | 548,225 | 2013-11-01 18:55:05 | https://stackoverflow.com/q/19733437 | https://stackoverflow.com/a/19733465 | <p>This is problem:</p>
<pre><code>if [[$variable == $blanko]];
</code></pre>
<p>Spaces are required inside square brackets, use it like this:</p>
<pre><code>[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
</code></pre>
| <p>This is problem:</p> <pre><code>if [[$variable == $blanko]]; </code></pre> <p>Spaces are required inside square brackets, use it like this:</p> <pre><code>[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable" </code></pre> | 139, 367, 387, 2773, 2936 | bash, compare, if-statement, string, syntax | <h1>Getting "command not found" error while comparing two strings in Bash</h1>
<p>My whole script is currently this:</p>
<pre><code>#!/bin/sh
clear;
blanko="";
# Dummy-Variablen
variable=Testvariable;
if [[$variable == $blanko]];
then
echo "Nichts da!"
else
echo $variable ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 838 | bash | # Getting "command not found" error while comparing two strings in Bash
My whole script is currently this:
```
#!/bin/sh
clear;
blanko="";
# Dummy-Variablen
variable=Testvariable;
if [[$variable == $blanko]];
then
echo "Nichts da!"
else
echo $variable
fi
```
and if I enter
```
TestSelect... | This is problem:
```
if [[$variable == $blanko]];
```
Spaces are required inside square brackets, use it like this:
```
[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
``` |
31506158 | Running Openssl from a bash script on windows - Subject does not start with '/' | 103 | 2015-07-19 21:32:59 | <p>In my script I have:</p>
<pre><code>openssl req \
-x509 \
-new \
-nodes \
-key certs/ca/my-root-ca.key.pem \
-days 3652 \
-out certs/ca/my-root-ca.crt.pem \
-subj "/C=GB/ST=someplace/L=Provo/O=Achme/CN=${FQDN}"
</code></pre>
<p>Running this on Windows in Git Bash 3.1 gives:</p>
<pre><code>Subject do... | 59,258 | 981,351 | 2018-12-08 04:59:38 | 31,990,313 | 246 | 2015-08-13 13:58:23 | 3,010,611 | 2015-08-13 14:11:57 | https://stackoverflow.com/q/31506158 | https://stackoverflow.com/a/31990313 | <p>This issue is specific to <strong>MinGW/MSYS</strong> which is commonly used as part of the <strong>Git for Windows</strong> package.</p>
<p>The solution is to pass the <code>-subj</code> argument with leading <code>//</code> (double forward slashes) and then use <code>\</code> (backslashes) to separate the key/val... | <p>This issue is specific to <strong>MinGW/MSYS</strong> which is commonly used as part of the <strong>Git for Windows</strong> package.</p> <p>The solution is to pass the <code>-subj</code> argument with leading <code>//</code> (double forward slashes) and then use <code>\</code> (backslashes) to separate the key/val... | 64, 139, 387, 1999 | bash, openssl, string, windows | <h1>Running Openssl from a bash script on windows - Subject does not start with '/'</h1>
<p>In my script I have:</p>
<pre><code>openssl req \
-x509 \
-new \
-nodes \
-key certs/ca/my-root-ca.key.pem \
-days 3652 \
-out certs/ca/my-root-ca.crt.pem \
-subj "/C=GB/ST=someplace/L=Provo/O=Achme/CN=${FQDN}"
</... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 839 | bash | # Running Openssl from a bash script on windows - Subject does not start with '/'
In my script I have:
```
openssl req \
-x509 \
-new \
-nodes \
-key certs/ca/my-root-ca.key.pem \
-days 3652 \
-out certs/ca/my-root-ca.crt.pem \
-subj "/C=GB/ST=someplace/L=Provo/O=Achme/CN=${FQDN}"
```
Running this on W... | This issue is specific to **MinGW/MSYS** which is commonly used as part of the **Git for Windows** package.
The solution is to pass the `-subj` argument with leading `//` (double forward slashes) and then use `\` (backslashes) to separate the key/value pairs. Like this:
```
"//O=Org\CN=Name"
```
This will then be ma... |
8529181 | Which terminal command to get just IP address and nothing else? | 204 | 2011-12-16 02:37:25 | <p>I'm trying to use just the IP address (inet) as a parameter in a script I wrote.</p>
<p>Is there an easy way in a unix terminal to get just the IP address, rather than looking through <code>ifconfig</code>? </p>
| 386,391 | 781,303 | 2024-07-30 15:55:56 | 8,529,217 | 245 | 2011-12-16 02:46:35 | 905,093 | 2011-12-29 23:45:16 | https://stackoverflow.com/q/8529181 | https://stackoverflow.com/a/8529217 | <p>You can write a script that only return the IP like:</p>
<pre><code>/sbin/ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}'
</code></pre>
<p>For MAC:</p>
<pre><code>ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2
</code></pre>
<p>Or for linux system</p>
<pre><code>hostname -i | awk '{... | <p>You can write a script that only return the IP like:</p> <pre><code>/sbin/ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}' </code></pre> <p>For MAC:</p> <pre><code>ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2 </code></pre> <p>Or for linux system</p> <pre><code>hostname -i | awk '{... | 34, 390, 7455 | ip-address, shell, unix | <h1>Which terminal command to get just IP address and nothing else?</h1>
<p>I'm trying to use just the IP address (inet) as a parameter in a script I wrote.</p>
<p>Is there an easy way in a unix terminal to get just the IP address, rather than looking through <code>ifconfig</code>? </p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 840 | bash | # Which terminal command to get just IP address and nothing else?
I'm trying to use just the IP address (inet) as a parameter in a script I wrote.
Is there an easy way in a unix terminal to get just the IP address, rather than looking through `ifconfig`? | You can write a script that only return the IP like:
```
/sbin/ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}'
```
For MAC:
```
ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2
```
Or for linux system
```
hostname -i | awk '{print $3}' # Ubuntu
hostname -i # Debian
``` |
3793126 | Colors with unix command "watch"? | 201 | 2010-09-25 08:55:25 | <p>Some commands that I use display colors, but when I use them with watch the colors disappears:</p>
<pre><code>watch -n 1 node file.js
</code></pre>
<p>Is it possible to have the colors back on somehow?</p>
| 85,039 | 224,922 | 2023-04-06 13:38:00 | 3,794,222 | 245 | 2010-09-25 14:37:18 | 26,428 | 2023-04-06 13:38:00 | https://stackoverflow.com/q/3793126 | https://stackoverflow.com/a/3794222 | <p>Some newer versions of <code>watch</code> now support color.</p>
<p>For example <code>watch --color ls -ahl --color</code>.</p>
<p><a href="https://stackoverflow.com/questions/2417824/how-can-i-make-the-watch-command-interpret-vt100-sequences">Related</a>.</p>
<p>If you're seeing monochrome output when you expect co... | <p>Some newer versions of <code>watch</code> now support color.</p> <p>For example <code>watch --color ls -ahl --color</code>.</p> <p><a href="https://stackoverflow.com/questions/2417824/how-can-i-make-the-watch-command-interpret-vt100-sequences">Related</a>.</p> <p>If you're seeing monochrome output when you expect co... | 58, 387, 549, 11274 | bash, linux, ubuntu, watch | <h1>Colors with unix command "watch"?</h1>
<p>Some commands that I use display colors, but when I use them with watch the colors disappears:</p>
<pre><code>watch -n 1 node file.js
</code></pre>
<p>Is it possible to have the colors back on somehow?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 841 | bash | # Colors with unix command "watch"?
Some commands that I use display colors, but when I use them with watch the colors disappears:
```
watch -n 1 node file.js
```
Is it possible to have the colors back on somehow? | Some newer versions of `watch` now support color.
For example `watch --color ls -ahl --color`.
[Related](https://stackoverflow.com/questions/2417824/how-can-i-make-the-watch-command-interpret-vt100-sequences).
If you're seeing monochrome output when you expect color, it's likely that the command you're running withi... |
10633564 | How does one change the language of the command line interface of Git? | 159 | 2012-05-17 10:03:49 | <p>I’d like to change the language of git (to English) in my Linux installation without changing the language for other programs and couldn’t find the settings.
How to do it?</p>
| 106,218 | 905,686 | 2024-05-22 09:56:45 | 10,872,202 | 245 | 2012-06-03 16:56:13 | 906,658 | 2020-10-22 19:44:50 | https://stackoverflow.com/q/10633564 | https://stackoverflow.com/a/10872202 | <p>Add these lines to your <code>~/.bashrc</code>, <code>~/.bash_profile</code> or <code>~/.zprofile</code> to force git to display all messages in English:</p>
<pre><code># Set Git language to English
#alias git='LANG=en_US git'
alias git='LANG=en_GB git'
</code></pre>
<p>The alias needs to override <code>LC_ALL</code... | <p>Add these lines to your <code>~/.bashrc</code>, <code>~/.bash_profile</code> or <code>~/.zprofile</code> to force git to display all messages in English:</p> <pre><code># Set Git language to English #alias git='LANG=en_US git' alias git='LANG=en_GB git' </code></pre> <p>The alias needs to override <code>LC_ALL</code... | 119, 387, 1921, 2780, 9013 | bash, environment-variables, git, locale, localization | <h1>How does one change the language of the command line interface of Git?</h1>
<p>I’d like to change the language of git (to English) in my Linux installation without changing the language for other programs and couldn’t find the settings.
How to do it?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 842 | bash | # How does one change the language of the command line interface of Git?
I’d like to change the language of git (to English) in my Linux installation without changing the language for other programs and couldn’t find the settings.
How to do it? | Add these lines to your `~/.bashrc`, `~/.bash_profile` or `~/.zprofile` to force git to display all messages in English:
```
# Set Git language to English
#alias git='LANG=en_US git'
alias git='LANG=en_GB git'
```
The alias needs to override `LC_ALL` on some systems, when the environment variable `LC_ALL` is set, whi... |
57131654 | Using UTF-8 Encoding (CHCP 65001) in Command Prompt / Windows Powershell (Windows 10) | 140 | 2019-07-21 08:43:49 | <p>I've been forcing the usage of <code>chcp 65001</code> in Command Prompt and Windows Powershell for some time now, but judging by Q&A posts on SO and several other communities it <a href="https://stackoverflow.com/a/388500/6834127">seems like a dangerous and inefficient solution</a>. Does Microsoft provide an im... | 301,475 | 6,834,127 | 2025-07-29 18:29:54 | 57,134,096 | 245 | 2019-07-21 14:26:06 | 45,375 | 2025-07-29 18:29:54 | https://stackoverflow.com/q/57131654 | https://stackoverflow.com/a/57134096 |
<p>Note:</p>
<ul>
<li><p>This answer shows how to switch the <strong>character <em>encoding</em></strong> in <strong>Windows</strong> consoles (terminals) to (BOM-less) <strong>UTF-8 <em>system-wide</em></strong> (code page <code>65001</code>), so that <em>shells</em> such as <code>cmd.exe</code> and PowerShell prope... | <p>Note:</p> <ul> <li><p>This answer shows how to switch the <strong>character <em>encoding</em></strong> in <strong>Windows</strong> consoles (terminals) to (BOM-less) <strong>UTF-8 <em>system-wide</em></strong> (code page <code>65001</code>), so that <em>shells</em> such as <code>cmd.exe</code> and PowerShell prope... | 64, 526, 8944, 10631 | console-application, powershell, utf-8, windows | <h1>Using UTF-8 Encoding (CHCP 65001) in Command Prompt / Windows Powershell (Windows 10)</h1>
<p>I've been forcing the usage of <code>chcp 65001</code> in Command Prompt and Windows Powershell for some time now, but judging by Q&A posts on SO and several other communities it <a href="https://stackoverflow.com/a/38... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 843 | bash | # Using UTF-8 Encoding (CHCP 65001) in Command Prompt / Windows Powershell (Windows 10)
I've been forcing the usage of `chcp 65001` in Command Prompt and Windows Powershell for some time now, but judging by Q&A posts on SO and several other communities it [seems like a dangerous and inefficient solution](https://stack... | Note:
- This answer shows how to switch the **character *encoding*** in **Windows** consoles (terminals) to (BOM-less) **UTF-8 *system-wide*** (code page `65001`), so that *shells* such as `cmd.exe` and PowerShell properly *encode and decode* characters (text) when communicating with *external (console) programs* with... |
78497 | Design patterns or best practices for shell scripts | 187 | 2008-09-17 00:00:03 | <p>Does anyone know of any resources that talk about best practices or design patterns for shell scripts (sh, bash etc.)?</p>
| 83,257 | 14,437 | 2022-12-22 10:29:13 | 739,034 | 244 | 2009-04-10 22:14:48 | 78,374 | 2022-12-22 10:29:13 | https://stackoverflow.com/q/78497 | https://stackoverflow.com/a/739034 | <p>I wrote quite complex shell scripts and my first suggestion is "don't". The reason is that is fairly easy to make a small mistake that hinders your script, or even make it dangerous.</p>
<p>That said, I don't have other resources to pass you but my personal experience.
Here is what I normally do, which is ... | <p>I wrote quite complex shell scripts and my first suggestion is "don't". The reason is that is fairly easy to make a small mistake that hinders your script, or even make it dangerous.</p> <p>That said, I don't have other resources to pass you but my personal experience. Here is what I normally do, which is ... | 387, 390, 2433 | bash, design-patterns, shell | <h1>Design patterns or best practices for shell scripts</h1>
<p>Does anyone know of any resources that talk about best practices or design patterns for shell scripts (sh, bash etc.)?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 844 | bash | # Design patterns or best practices for shell scripts
Does anyone know of any resources that talk about best practices or design patterns for shell scripts (sh, bash etc.)? | I wrote quite complex shell scripts and my first suggestion is "don't". The reason is that is fairly easy to make a small mistake that hinders your script, or even make it dangerous.
That said, I don't have other resources to pass you but my personal experience.
Here is what I normally do, which is overkill, but tends... |
7328223 | UNIX export command | 167 | 2011-09-07 02:18:26 | <p>I am trying to understand the use of <code>export</code> command. </p>
<p>I tried using <code>man export</code>, but there is no manual for this command. </p>
<p>Can anyone please help me out understanding the use of <code>export</code> in UNIX?</p>
| 233,357 | 682,869 | 2015-10-01 21:26:12 | 7,328,289 | 244 | 2011-09-07 02:31:12 | 68,587 | 2011-09-07 02:31:12 | https://stackoverflow.com/q/7328223 | https://stackoverflow.com/a/7328289 | <p>When you execute a program the child program inherits its environment variables from the parent. For instance if <code>$HOME</code> is set to <code>/root</code> in the parent then the child's <code>$HOME</code> variable is also set to <code>/root</code>.</p>
<p>This only applies to environment variable that are mar... | <p>When you execute a program the child program inherits its environment variables from the parent. For instance if <code>$HOME</code> is set to <code>/root</code> in the parent then the child's <code>$HOME</code> variable is also set to <code>/root</code>.</p> <p>This only applies to environment variable that are mar... | 34, 387, 390 | bash, shell, unix | <h1>UNIX export command</h1>
<p>I am trying to understand the use of <code>export</code> command. </p>
<p>I tried using <code>man export</code>, but there is no manual for this command. </p>
<p>Can anyone please help me out understanding the use of <code>export</code> in UNIX?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 845 | bash | # UNIX export command
I am trying to understand the use of `export` command.
I tried using `man export`, but there is no manual for this command.
Can anyone please help me out understanding the use of `export` in UNIX? | When you execute a program the child program inherits its environment variables from the parent. For instance if `$HOME` is set to `/root` in the parent then the child's `$HOME` variable is also set to `/root`.
This only applies to environment variable that are marked for export. If you set a variable at the command-l... |
9725521 | How to get the Parent's parent directory in Powershell? | 158 | 2012-03-15 18:03:20 | <p>So if I have a directory stored in a variable, say:</p>
<pre><code>$scriptPath = (Get-ScriptDirectory);
</code></pre>
<p>Now I would like to find the directory <em>two</em> parent levels up.</p>
<p>I need a nice way of doing:</p>
<pre><code>$parentPath = Split-Path -parent $scriptPath
$rootPath = Split-Path -par... | 300,643 | 16,642 | 2024-06-13 21:55:21 | 9,725,667 | 244 | 2012-03-15 18:13:11 | 149,458 | 2016-12-29 11:02:01 | https://stackoverflow.com/q/9725521 | https://stackoverflow.com/a/9725667 | <h2>Version for a directory</h2>
<p><code>get-item</code> is your friendly helping hand here. </p>
<pre><code>(get-item $scriptPath ).parent.parent
</code></pre>
<p>If you Want the string only</p>
<pre><code>(get-item $scriptPath ).parent.parent.FullName
</code></pre>
<h2>Version for a file</h2>
<p>If <code>$scri... | <h2>Version for a directory</h2> <p><code>get-item</code> is your friendly helping hand here. </p> <pre><code>(get-item $scriptPath ).parent.parent </code></pre> <p>If you Want the string only</p> <pre><code>(get-item $scriptPath ).parent.parent.FullName </code></pre> <h2>Version for a file</h2> <p>If <code>$scri... | 526, 531, 6268 | path, powershell, scripting | <h1>How to get the Parent's parent directory in Powershell?</h1>
<p>So if I have a directory stored in a variable, say:</p>
<pre><code>$scriptPath = (Get-ScriptDirectory);
</code></pre>
<p>Now I would like to find the directory <em>two</em> parent levels up.</p>
<p>I need a nice way of doing:</p>
<pre><code>$parent... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 846 | bash | # How to get the Parent's parent directory in Powershell?
So if I have a directory stored in a variable, say:
```
$scriptPath = (Get-ScriptDirectory);
```
Now I would like to find the directory *two* parent levels up.
I need a nice way of doing:
```
$parentPath = Split-Path -parent $scriptPath
$rootPath = Split-Pa... | ## Version for a directory
`get-item` is your friendly helping hand here.
```
(get-item $scriptPath ).parent.parent
```
If you Want the string only
```
(get-item $scriptPath ).parent.parent.FullName
```
## Version for a file
If `$scriptPath` points to a file then you have to call `Directory` property on it first,... |
10574794 | How to list only files and not directories of a directory Bash? | 183 | 2012-05-13 20:04:37 | <p>How can I list all the files of one folder but not their folders or subfiles. In other words: How can I list only the files?</p>
| 251,867 | 632,472 | 2025-01-08 09:55:56 | 10,574,806 | 243 | 2012-05-13 20:06:35 | 124,178 | 2012-05-13 20:06:35 | https://stackoverflow.com/q/10574794 | https://stackoverflow.com/a/10574806 | <p>Using <code>find</code>:</p>
<pre><code>find . -maxdepth 1 -type f
</code></pre>
<p>Using the <code>-maxdepth 1</code> option ensures that you only look in the current directory (or, if you replace the <code>.</code> with some path, that directory). If you want a full recursive listing of all files in that and sub... | <p>Using <code>find</code>:</p> <pre><code>find . -maxdepth 1 -type f </code></pre> <p>Using the <code>-maxdepth 1</code> option ensures that you only look in the current directory (or, if you replace the <code>.</code> with some path, that directory). If you want a full recursive listing of all files in that and sub... | 387, 24582 | bash, file-listing | <h1>How to list only files and not directories of a directory Bash?</h1>
<p>How can I list all the files of one folder but not their folders or subfiles. In other words: How can I list only the files?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 847 | bash | # How to list only files and not directories of a directory Bash?
How can I list all the files of one folder but not their folders or subfiles. In other words: How can I list only the files? | Using `find`:
```
find . -maxdepth 1 -type f
```
Using the `-maxdepth 1` option ensures that you only look in the current directory (or, if you replace the `.` with some path, that directory). If you want a full recursive listing of all files in that and subdirectories, just remove that option. |
1654021 | How can I delete a newline if it is the last character in a file? | 182 | 2009-10-31 10:42:19 | <p>I have some files that I'd like to delete the last newline if it is the last character in a file. <code>od -c</code> shows me that the command I run does write the file with a trailing new line:</p>
<pre><code>0013600 n t > \n
</code></pre>
<p>I've tried a few tricks with sed but the best I could think o... | 119,331 | 998,078 | 2022-10-02 23:09:28 | 1,654,042 | 243 | 2009-10-31 10:55:12 | 153,545 | 2015-04-08 16:53:20 | https://stackoverflow.com/q/1654021 | https://stackoverflow.com/a/1654042 | <pre><code>perl -pe 'chomp if eof' filename >filename2
</code></pre>
<p>or, to edit the file in place:</p>
<pre><code>perl -pi -e 'chomp if eof' filename
</code></pre>
<p><sup>[Editor's note: <code>-pi -e</code> was originally <code>-pie</code>, but, as noted by several commenters and explained by @hvd, the latte... | <pre><code>perl -pe 'chomp if eof' filename >filename2 </code></pre> <p>or, to edit the file in place:</p> <pre><code>perl -pi -e 'chomp if eof' filename </code></pre> <p><sup>[Editor's note: <code>-pi -e</code> was originally <code>-pie</code>, but, as noted by several commenters and explained by @hvd, the latte... | 58, 390, 580, 990, 5282 | awk, linux, perl, sed, shell | <h1>How can I delete a newline if it is the last character in a file?</h1>
<p>I have some files that I'd like to delete the last newline if it is the last character in a file. <code>od -c</code> shows me that the command I run does write the file with a trailing new line:</p>
<pre><code>0013600 n t > \n
</co... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 848 | bash | # How can I delete a newline if it is the last character in a file?
I have some files that I'd like to delete the last newline if it is the last character in a file. `od -c` shows me that the command I run does write the file with a trailing new line:
```
0013600 n t > \n
```
I've tried a few tricks with sed b... | ```
perl -pe 'chomp if eof' filename >filename2
```
or, to edit the file in place:
```
perl -pi -e 'chomp if eof' filename
```
[Editor's note: `-pi -e` was originally `-pie`, but, as noted by several commenters and explained by @hvd, the latter doesn't work.]
This was described as a 'perl blasphemy' on the awk webs... |
11107428 | How can I force Powershell to return an array when a call only returns one object? | 214 | 2012-06-19 18:49:26 | <p>I'm using Powershell to set up IIS bindings on a web server, and having a problem with the following code:</p>
<pre><code>$serverIps = gwmi Win32_NetworkAdapterConfiguration
| Where { $_.IPAddress }
| Select -Expand IPAddress
| Where { $_ -like '*.*.*.*' }
| Sort
if ($serverIps.length -le 1) {
... | 103,496 | 5,017 | 2023-06-02 18:21:21 | 11,107,477 | 242 | 2012-06-19 18:52:10 | 397,952 | 2012-06-19 18:58:20 | https://stackoverflow.com/q/11107428 | https://stackoverflow.com/a/11107477 | <p><strong>Define the variable as an array in one of two ways...</strong></p>
<p>Wrap your piped commands in parentheses with an <code>@</code> at the beginning:</p>
<pre><code>$serverIps = @(gwmi Win32_NetworkAdapterConfiguration
| Where { $_.IPAddress }
| Select -Expand IPAddress
| Where { $_ -like '... | <p><strong>Define the variable as an array in one of two ways...</strong></p> <p>Wrap your piped commands in parentheses with an <code>@</code> at the beginning:</p> <pre><code>$serverIps = @(gwmi Win32_NetworkAdapterConfiguration | Where { $_.IPAddress } | Select -Expand IPAddress | Where { $_ -like '... | 526 | powershell | <h1>How can I force Powershell to return an array when a call only returns one object?</h1>
<p>I'm using Powershell to set up IIS bindings on a web server, and having a problem with the following code:</p>
<pre><code>$serverIps = gwmi Win32_NetworkAdapterConfiguration
| Where { $_.IPAddress }
| Select -Expan... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 849 | bash | # How can I force Powershell to return an array when a call only returns one object?
I'm using Powershell to set up IIS bindings on a web server, and having a problem with the following code:
```
$serverIps = gwmi Win32_NetworkAdapterConfiguration
| Where { $_.IPAddress }
| Select -Expand IPAddress
| W... | **Define the variable as an array in one of two ways...**
Wrap your piped commands in parentheses with an `@` at the beginning:
```
$serverIps = @(gwmi Win32_NetworkAdapterConfiguration
| Where { $_.IPAddress }
| Select -Expand IPAddress
| Where { $_ -like '*.*.*.*' }
| Sort)
```
Specify the data... |
11065077 | The 'eval' command in Bash and its typical uses | 207 | 2012-06-16 16:13:41 | <p>After reading the <a href="https://linux.die.net/man/1/bash" rel="noreferrer">Bash man pages</a> and with respect to <a href="http://www.unix.com/shell-programming-scripting/66063-eval-shell-scripting.html" rel="noreferrer">this post</a>, I am still having trouble understanding what exactly the <code>eval</code> com... | 500,026 | 178,728 | 2022-12-18 10:00:42 | 11,065,196 | 242 | 2012-06-16 16:32:37 | 387,076 | 2019-03-29 17:42:45 | https://stackoverflow.com/q/11065077 | https://stackoverflow.com/a/11065196 | <p><code>eval</code> takes a string as its argument, and evaluates it as if you'd typed that string on a command line. (If you pass several arguments, they are first joined with spaces between them.)</p>
<p><code>${$n}</code> is a syntax error in bash. Inside the braces, you can only have a variable name, with some po... | <p><code>eval</code> takes a string as its argument, and evaluates it as if you'd typed that string on a command line. (If you pass several arguments, they are first joined with spaces between them.)</p> <p><code>${$n}</code> is a syntax error in bash. Inside the braces, you can only have a variable name, with some po... | 58, 387, 390, 531, 4413 | bash, eval, linux, scripting, shell | <h1>The 'eval' command in Bash and its typical uses</h1>
<p>After reading the <a href="https://linux.die.net/man/1/bash" rel="noreferrer">Bash man pages</a> and with respect to <a href="http://www.unix.com/shell-programming-scripting/66063-eval-shell-scripting.html" rel="noreferrer">this post</a>, I am still having tro... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 850 | bash | # The 'eval' command in Bash and its typical uses
After reading the [Bash man pages](https://linux.die.net/man/1/bash) and with respect to [this post](http://www.unix.com/shell-programming-scripting/66063-eval-shell-scripting.html), I am still having trouble understanding what exactly the `eval` command does and which... | `eval` takes a string as its argument, and evaluates it as if you'd typed that string on a command line. (If you pass several arguments, they are first joined with spaces between them.)
`${$n}` is a syntax error in bash. Inside the braces, you can only have a variable name, with some possible prefix and suffixes, but ... |
13338870 | What does "&" at the end of a linux command mean? | 179 | 2012-11-12 05:37:22 | <p>I am a system administrator and I have been asked to run a linux script to clean the system.</p>
<p>The command is this:</p>
<pre><code>perl script.pl > output.log &
</code></pre>
<p>so this command is ending with a <code>&</code> sign, is there any special significance of it?</p>
<p>I have basic know... | 350,887 | 1,529,267 | 2024-11-11 09:40:54 | 13,338,895 | 242 | 2012-11-12 05:41:36 | 641,451 | 2024-11-11 09:40:54 | https://stackoverflow.com/q/13338870 | https://stackoverflow.com/a/13338895 | <p>The <code>&</code> makes the command run in the background.</p>
<p>From <a href="https://www.gnu.org/software/bash/manual/bash.html#Lists-of-Commands" rel="noreferrer"><code>man bash</code></a>:</p>
<blockquote>
<p>If a command is terminated by the control operator <code>&</code>, the shell executes the comm... | <p>The <code>&</code> makes the command run in the background.</p> <p>From <a href="https://www.gnu.org/software/bash/manual/bash.html#Lists-of-Commands" rel="noreferrer"><code>man bash</code></a>:</p> <blockquote> <p>If a command is terminated by the control operator <code>&</code>, the shell executes the comm... | 34, 390 | shell, unix | <h1>What does "&" at the end of a linux command mean?</h1>
<p>I am a system administrator and I have been asked to run a linux script to clean the system.</p>
<p>The command is this:</p>
<pre><code>perl script.pl > output.log &
</code></pre>
<p>so this command is ending with a <code>&</code> sign, is ther... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 851 | bash | # What does "&" at the end of a linux command mean?
I am a system administrator and I have been asked to run a linux script to clean the system.
The command is this:
```
perl script.pl > output.log &
```
so this command is ending with a `&` sign, is there any special significance of it?
I have basic knowledge of s... | The `&` makes the command run in the background.
From [`man bash`](https://www.gnu.org/software/bash/manual/bash.html#Lists-of-Commands):
> If a command is terminated by the control operator `&`, the shell executes the command in the background in a subshell. [...] The shell does not wait for the command to finish, a... |
24263291 | Define a Makefile variable using a ENV variable or a default value | 147 | 2014-06-17 12:02:30 | <p>I am trying to do a simple thing:</p>
<pre><code>TMPDIR ?= /tmp
test:
@echo $(TMPDIR)
</code></pre>
<p>This works if I run:</p>
<pre><code>$ make test
/tmp
</code></pre>
<p>It also works if I run:</p>
<pre><code>$ make test -e TMPDIR=~/tmp
/home/user/tmp
</code></pre>
<p>What can I do to also have it work... | 128,453 | 186,202 | 2023-09-26 06:27:50 | 24,264,930 | 242 | 2014-06-17 13:18:09 | 939,557 | 2023-09-26 06:27:50 | https://stackoverflow.com/q/24263291 | https://stackoverflow.com/a/24264930 | <p>To follow up on my comments above, here's an example:</p>
<pre><code>T ?= foo
all:
$(info T is $(T))
</code></pre>
<p>Now if I run the Makefile in various ways, it behaves as we expect (I get <code>foo</code> only if I don't set <code>T</code> either on the command line or environment):</p>
<pre><code>$ make... | <p>To follow up on my comments above, here's an example:</p> <pre><code>T ?= foo all: $(info T is $(T)) </code></pre> <p>Now if I run the Makefile in various ways, it behaves as we expect (I get <code>foo</code> only if I don't set <code>T</code> either on the command line or environment):</p> <pre><code>$ make... | 387, 390, 4301, 9013 | bash, environment-variables, makefile, shell | <h1>Define a Makefile variable using a ENV variable or a default value</h1>
<p>I am trying to do a simple thing:</p>
<pre><code>TMPDIR ?= /tmp
test:
@echo $(TMPDIR)
</code></pre>
<p>This works if I run:</p>
<pre><code>$ make test
/tmp
</code></pre>
<p>It also works if I run:</p>
<pre><code>$ make test -e TMPD... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 852 | bash | # Define a Makefile variable using a ENV variable or a default value
I am trying to do a simple thing:
```
TMPDIR ?= /tmp
test:
@echo $(TMPDIR)
```
This works if I run:
```
$ make test
/tmp
```
It also works if I run:
```
$ make test -e TMPDIR=~/tmp
/home/user/tmp
```
What can I do to also have it works for... | To follow up on my comments above, here's an example:
```
T ?= foo
all:
$(info T is $(T))
```
Now if I run the Makefile in various ways, it behaves as we expect (I get `foo` only if I don't set `T` either on the command line or environment):
```
$ make
T is foo
$ make T=bar
T is bar
$ T=bar make
T is bar
`... |
30123463 | How to hide wget output in Linux? | 139 | 2015-05-08 12:07:07 | <p>I don't want to see any message when I use <code>wget</code>. I want to suppress all the output it normally produces on the screen.</p>
<p>How can I do it?</p>
| 156,461 | 4,037,465 | 2021-04-28 22:22:27 | 30,123,495 | 242 | 2015-05-08 12:08:33 | 1,983,854 | 2015-05-08 12:09:34 | https://stackoverflow.com/q/30123463 | https://stackoverflow.com/a/30123495 | <p>Why don't you use <code>-q</code>?</p>
<p>From <code>man wget</code>:</p>
<pre><code>-q
--quiet
Turn off Wget's output.
</code></pre>
<h3>Test</h3>
<pre><code>$ wget www.google.com
--2015-05-08 14:07:42-- http://www.google.com/
Resolving www.google.com (www.google.com)...
(...)
HTTP request sent, awaiting... | <p>Why don't you use <code>-q</code>?</p> <p>From <code>man wget</code>:</p> <pre><code>-q --quiet Turn off Wget's output. </code></pre> <h3>Test</h3> <pre><code>$ wget www.google.com --2015-05-08 14:07:42-- http://www.google.com/ Resolving www.google.com (www.google.com)... (...) HTTP request sent, awaiting... | 58, 387, 5583 | bash, linux, wget | <h1>How to hide wget output in Linux?</h1>
<p>I don't want to see any message when I use <code>wget</code>. I want to suppress all the output it normally produces on the screen.</p>
<p>How can I do it?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 853 | bash | # How to hide wget output in Linux?
I don't want to see any message when I use `wget`. I want to suppress all the output it normally produces on the screen.
How can I do it? | Why don't you use `-q`?
From `man wget`:
```
-q
--quiet
Turn off Wget's output.
```
### Test
```
$ wget www.google.com
--2015-05-08 14:07:42-- http://www.google.com/
Resolving www.google.com (www.google.com)...
(...)
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘i... |
43235179 | How to execute ssh-keygen without prompt | 135 | 2017-04-05 15:11:19 | <p>I want to automate generate a pair of ssh key using shell script on Centos7, and I have tried</p>
<pre><code>yes "y" | ssh-keygen -t rsa
echo "\n\n\n" | ssh-keygen...
echo | ssh-keygen..
</code></pre>
<p>all of these command doesn't work, just input one 'enter' and the shell script stopped on "Enter passphrase (em... | 125,711 | 5,412,341 | 2024-11-15 13:44:02 | 43,235,320 | 242 | 2017-04-05 15:18:21 | 1,200,821 | 2021-07-30 21:41:09 | https://stackoverflow.com/q/43235179 | https://stackoverflow.com/a/43235320 | <p>We need to accomplish <strong>two steps</strong> automatically:</p>
<ol>
<li><p><strong>Enter a passphrase</strong>. Use the <code>-N</code> flag (void string for this example):</p>
<p><code>ssh-keygen -t rsa -N ''</code></p>
</li>
<li><p><strong>Overwrite the key file</strong>:</p>
</li>
</ol>
<p>Use <code>-f</code... | <p>We need to accomplish <strong>two steps</strong> automatically:</p> <ol> <li><p><strong>Enter a passphrase</strong>. Use the <code>-N</code> flag (void string for this example):</p> <p><code>ssh-keygen -t rsa -N ''</code></p> </li> <li><p><strong>Overwrite the key file</strong>:</p> </li> </ol> <p>Use <code>-f</code... | 58, 386, 387, 390 | bash, linux, shell, ssh | <h1>How to execute ssh-keygen without prompt</h1>
<p>I want to automate generate a pair of ssh key using shell script on Centos7, and I have tried</p>
<pre><code>yes "y" | ssh-keygen -t rsa
echo "\n\n\n" | ssh-keygen...
echo | ssh-keygen..
</code></pre>
<p>all of these command doesn't work, just input one 'enter' and... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 854 | bash | # How to execute ssh-keygen without prompt
I want to automate generate a pair of ssh key using shell script on Centos7, and I have tried
```
yes "y" | ssh-keygen -t rsa
echo "\n\n\n" | ssh-keygen...
echo | ssh-keygen..
```
all of these command doesn't work, just input one 'enter' and the shell script stopped on "Ent... | We need to accomplish **two steps** automatically:
1. **Enter a passphrase**. Use the `-N` flag (void string for this example):
`ssh-keygen -t rsa -N ''`
2. **Overwrite the key file**:
Use `-f` to enter the path (in this example `id_rsa`) plus a **here-string** to answer *yes* to the following question:
```
ssh-... |
5386482 | How to run the sftp command with a password from Bash script? | 242 | 2011-03-22 03:40:24 | <p>I need to transfer a log file to a remote host using <a href="http://en.wikipedia.org/wiki/Secure_file_transfer_program" rel="noreferrer">sftp</a> from a Linux host. I have been provided credentials for the same from my operations group. However, since I don't have control over other host, I cannot generate and shar... | 1,052,813 | 548,225 | 2025-10-21 21:41:59 | 5,386,587 | 241 | 2011-03-22 03:54:15 | 548,225 | 2024-05-08 10:14:45 | https://stackoverflow.com/q/5386482 | https://stackoverflow.com/a/5386587 | <p>You have a few options other than using public key authentication:</p>
<ol>
<li>Use <a href="http://www.cyberciti.biz/faq/ssh-passwordless-login-with-keychain-for-scripts/" rel="noreferrer">keychain</a></li>
<li>Use <a href="http://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/" rel="norefe... | <p>You have a few options other than using public key authentication:</p> <ol> <li>Use <a href="http://www.cyberciti.biz/faq/ssh-passwordless-login-with-keychain-for-scripts/" rel="noreferrer">keychain</a></li> <li>Use <a href="http://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/" rel="norefe... | 34, 386, 387, 390, 1290 | bash, sftp, shell, ssh, unix | <h1>How to run the sftp command with a password from Bash script?</h1>
<p>I need to transfer a log file to a remote host using <a href="http://en.wikipedia.org/wiki/Secure_file_transfer_program" rel="noreferrer">sftp</a> from a Linux host. I have been provided credentials for the same from my operations group. However,... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 855 | bash | # How to run the sftp command with a password from Bash script?
I need to transfer a log file to a remote host using [sftp](http://en.wikipedia.org/wiki/Secure_file_transfer_program) from a Linux host. I have been provided credentials for the same from my operations group. However, since I don't have control over othe... | You have a few options other than using public key authentication:
1. Use [keychain](http://www.cyberciti.biz/faq/ssh-passwordless-login-with-keychain-for-scripts/)
2. Use [sshpass](http://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/) (less secured but probably that meets your requirement)
... |
4780203 | Deleting lines from one file which are in another file | 189 | 2011-01-24 09:03:32 | <p>I have a file <code>f1</code>:</p>
<pre><code>line1
line2
line3
line4
..
..
</code></pre>
<p>I want to delete all the lines which are in another file <code>f2</code>:</p>
<pre><code>line2
line8
..
..
</code></pre>
<p>I tried something with <code>cat</code> and <code>sed</code>, which wasn't even close to what I ... | 107,253 | 432,622 | 2022-01-24 19:11:10 | 4,780,220 | 241 | 2011-01-24 09:06:24 | 565,342 | 2019-03-12 12:49:30 | https://stackoverflow.com/q/4780203 | https://stackoverflow.com/a/4780220 | <p><code>grep -v -x -f f2 f1</code> should do the trick.</p>
<p>Explanation:</p>
<ul>
<li><code>-v</code> to select non-matching lines</li>
<li><code>-x</code> to match whole lines only</li>
<li><code>-f f2</code> to get patterns from <code>f2</code></li>
</ul>
<p>One can instead use <code>grep -F</code> or <code>fg... | <p><code>grep -v -x -f f2 f1</code> should do the trick.</p> <p>Explanation:</p> <ul> <li><code>-v</code> to select non-matching lines</li> <li><code>-x</code> to match whole lines only</li> <li><code>-f f2</code> to get patterns from <code>f2</code></li> </ul> <p>One can instead use <code>grep -F</code> or <code>fg... | 387, 531, 10327 | bash, scripting, sh | <h1>Deleting lines from one file which are in another file</h1>
<p>I have a file <code>f1</code>:</p>
<pre><code>line1
line2
line3
line4
..
..
</code></pre>
<p>I want to delete all the lines which are in another file <code>f2</code>:</p>
<pre><code>line2
line8
..
..
</code></pre>
<p>I tried something with <code>cat... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 856 | bash | # Deleting lines from one file which are in another file
I have a file `f1`:
```
line1
line2
line3
line4
..
..
```
I want to delete all the lines which are in another file `f2`:
```
line2
line8
..
..
```
I tried something with `cat` and `sed`, which wasn't even close to what I intended. How can I do this? | `grep -v -x -f f2 f1` should do the trick.
Explanation:
- `-v` to select non-matching lines
- `-x` to match whole lines only
- `-f f2` to get patterns from `f2`
One can instead use `grep -F` or `fgrep` to match *fixed strings* from `f2` rather than *patterns* (in case you want remove the lines in a "what you see if ... |
14549270 | Check if database exists in PostgreSQL using shell | 182 | 2013-01-27 16:10:18 | <p>I was wondering if anyone would be able to tell me about whether it is possible to use shell to check, if a PostgreSQL database exists.</p>
<p>I am making a shell script and I only want it to create the database if it doesn't already exist, but up to now, I haven't been able to see how to implement it.</p>
| 165,187 | 1,738,522 | 2024-09-04 15:29:44 | 16,783,253 | 241 | 2013-05-28 03:04:49 | 81,804 | 2021-02-01 05:07:16 | https://stackoverflow.com/q/14549270 | https://stackoverflow.com/a/16783253 | <p>Note/Update (2021): While this answer <em>works</em>, philosophically I agree with other comments that the right way to do this is to <em>ask Postgres</em>.</p>
<p>Check whether the other answers that have <code>psql -c</code> or <code>--command</code> in them are a better fit for your use case (e.g. <a href="https:... | <p>Note/Update (2021): While this answer <em>works</em>, philosophically I agree with other comments that the right way to do this is to <em>ask Postgres</em>.</p> <p>Check whether the other answers that have <code>psql -c</code> or <code>--command</code> in them are a better fit for your use case (e.g. <a href="https:... | 256, 390 | postgresql, shell | <h1>Check if database exists in PostgreSQL using shell</h1>
<p>I was wondering if anyone would be able to tell me about whether it is possible to use shell to check, if a PostgreSQL database exists.</p>
<p>I am making a shell script and I only want it to create the database if it doesn't already exist, but up to now, I... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 857 | bash | # Check if database exists in PostgreSQL using shell
I was wondering if anyone would be able to tell me about whether it is possible to use shell to check, if a PostgreSQL database exists.
I am making a shell script and I only want it to create the database if it doesn't already exist, but up to now, I haven't been a... | Note/Update (2021): While this answer *works*, philosophically I agree with other comments that the right way to do this is to *ask Postgres*.
Check whether the other answers that have `psql -c` or `--command` in them are a better fit for your use case (e.g. [Nicholas Grilly's](https://stackoverflow.com/a/34174754/818... |
11039876 | Multiplication on command line terminal | 137 | 2012-06-14 19:18:09 | <p>I'm using a serial terminal to provide input into our lab experiment. I found that using </p>
<pre><code>$ echo "5X5"
</code></pre>
<p>just returns a string of <code>"5X5"</code>. Is there a command to execute a multiplication operation?</p>
| 240,262 | 1,416,058 | 2022-11-07 22:30:55 | 11,039,905 | 241 | 2012-06-14 19:20:09 | 236,871 | 2014-08-17 12:55:03 | https://stackoverflow.com/q/11039876 | https://stackoverflow.com/a/11039905 | <p>Yes, you can use <a href="https://www.gnu.org/software/bash/manual/html_node/Arithmetic-Expansion.html" rel="noreferrer">bash's built-in Arithmetic Expansion <code>$(( ))</code></a> to do some simple maths</p>
<pre><code>$ echo "$((5 * 5))"
25
</code></pre>
<p>Check the <a href="https://www.gnu.org/software/bash/m... | <p>Yes, you can use <a href="https://www.gnu.org/software/bash/manual/html_node/Arithmetic-Expansion.html" rel="noreferrer">bash's built-in Arithmetic Expansion <code>$(( ))</code></a> to do some simple maths</p> <pre><code>$ echo "$((5 * 5))" 25 </code></pre> <p>Check the <a href="https://www.gnu.org/software/bash/m... | 34, 387, 1231 | bash, command-line, unix | <h1>Multiplication on command line terminal</h1>
<p>I'm using a serial terminal to provide input into our lab experiment. I found that using </p>
<pre><code>$ echo "5X5"
</code></pre>
<p>just returns a string of <code>"5X5"</code>. Is there a command to execute a multiplication operation?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 858 | bash | # Multiplication on command line terminal
I'm using a serial terminal to provide input into our lab experiment. I found that using
```
$ echo "5X5"
```
just returns a string of `"5X5"`. Is there a command to execute a multiplication operation? | Yes, you can use [bash's built-in Arithmetic Expansion `$(( ))`](https://www.gnu.org/software/bash/manual/html_node/Arithmetic-Expansion.html) to do some simple maths
```
$ echo "$((5 * 5))"
25
```
Check the [Shell Arithmetic section in the Bash Reference Manual](https://www.gnu.org/software/bash/manual/html_node/She... |
222414 | Asynchronous shell exec in PHP | 219 | 2008-10-21 16:02:10 | <p>I've got a PHP script that needs to invoke a shell script but doesn't care at all about the output. The shell script makes a number of SOAP calls and is slow to complete, so I don't want to slow down the PHP request while it waits for a reply. In fact, the PHP request should be able to exit without terminating the s... | 198,004 | 1,103 | 2022-09-12 23:37:54 | 222,445 | 240 | 2008-10-21 16:08:03 | 4,418 | 2013-11-20 16:59:51 | https://stackoverflow.com/q/222414 | https://stackoverflow.com/a/222445 | <p>If it "doesn't care about the output", couldn't the exec to the script be called with the <code>&</code> to background the process?</p>
<p><strong>EDIT</strong> - incorporating what @<a href="https://stackoverflow.com/users/1103/adamthehutt">AdamTheHut</a> commented to this post, you can add this to a call to <... | <p>If it "doesn't care about the output", couldn't the exec to the script be called with the <code>&</code> to background the process?</p> <p><strong>EDIT</strong> - incorporating what @<a href="https://stackoverflow.com/users/1103/adamthehutt">AdamTheHut</a> commented to this post, you can add this to a call to <... | 5, 390, 2443 | asynchronous, php, shell | <h1>Asynchronous shell exec in PHP</h1>
<p>I've got a PHP script that needs to invoke a shell script but doesn't care at all about the output. The shell script makes a number of SOAP calls and is slow to complete, so I don't want to slow down the PHP request while it waits for a reply. In fact, the PHP request should b... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 859 | bash | # Asynchronous shell exec in PHP
I've got a PHP script that needs to invoke a shell script but doesn't care at all about the output. The shell script makes a number of SOAP calls and is slow to complete, so I don't want to slow down the PHP request while it waits for a reply. In fact, the PHP request should be able to... | If it "doesn't care about the output", couldn't the exec to the script be called with the `&` to background the process?
**EDIT** - incorporating what @[AdamTheHut](https://stackoverflow.com/users/1103/adamthehutt) commented to this post, you can add this to a call to `exec`:
```
" > /dev/null 2>/dev/null &"
```
Tha... |
5260125 | What's the better (cleaner) way to ignore output in PowerShell? | 184 | 2011-03-10 13:15:51 | <p>Let's say you have a method or a cmdlet that returns something, but you don't want to use it and you don't want to output it. I found these two ways:</p>
<pre><code>Add-Item > $null
[void]Add-Item
Add-Item | Out-Null
</code></pre>
<p>What do you use? Which is the better/cleaner approach? Why?</p>
| 170,543 | 20,580 | 2025-05-30 14:26:52 | 5,263,780 | 240 | 2011-03-10 18:01:23 | 64,046 | 2017-10-16 23:30:17 | https://stackoverflow.com/q/5260125 | https://stackoverflow.com/a/5263780 | <p>I just did some tests of the four options that I know about.</p>
<pre><code>Measure-Command {$(1..1000) | Out-Null}
TotalMilliseconds : 76.211
Measure-Command {[Void]$(1..1000)}
TotalMilliseconds : 0.217
Measure-Command {$(1..1000) > $null}
TotalMilliseconds : 0.2478
Measure-Command {$null = $(1..1000)}
T... | <p>I just did some tests of the four options that I know about.</p> <pre><code>Measure-Command {$(1..1000) | Out-Null} TotalMilliseconds : 76.211 Measure-Command {[Void]$(1..1000)} TotalMilliseconds : 0.217 Measure-Command {$(1..1000) > $null} TotalMilliseconds : 0.2478 Measure-Command {$null = $(1..1000)} T... | 526, 694, 14375, 26698 | io-redirection, null, powershell, void | <h1>What's the better (cleaner) way to ignore output in PowerShell?</h1>
<p>Let's say you have a method or a cmdlet that returns something, but you don't want to use it and you don't want to output it. I found these two ways:</p>
<pre><code>Add-Item > $null
[void]Add-Item
Add-Item | Out-Null
</code></pre>
<p>Wha... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 860 | bash | # What's the better (cleaner) way to ignore output in PowerShell?
Let's say you have a method or a cmdlet that returns something, but you don't want to use it and you don't want to output it. I found these two ways:
```
Add-Item > $null
[void]Add-Item
Add-Item | Out-Null
```
What do you use? Which is the better/cl... | I just did some tests of the four options that I know about.
```
Measure-Command {$(1..1000) | Out-Null}
TotalMilliseconds : 76.211
Measure-Command {[Void]$(1..1000)}
TotalMilliseconds : 0.217
Measure-Command {$(1..1000) > $null}
TotalMilliseconds : 0.2478
Measure-Command {$null = $(1..1000)}
TotalMilliseconds ... |
10822790 | Can I call a function of a shell script from another shell script? | 174 | 2012-05-30 19:15:50 | <p>I have 2 shell scripts.</p>
<p>The second shell script contains following functions
<em>second.sh</em></p>
<pre><code>func1
func2
</code></pre>
<p>The first.sh will call the second shell script with some parameters and
will call func1 and func2 with some other parameters specific to that function.</p>
<p>Here ... | 225,245 | 1,399,894 | 2024-10-02 16:55:41 | 10,823,213 | 240 | 2012-05-30 19:47:42 | 548,225 | 2024-10-02 16:55:41 | https://stackoverflow.com/q/10822790 | https://stackoverflow.com/a/10823213 | <p>You can refactor your <code>second.sh</code> script like this:</p>
<pre><code>func1 () {
fun="$1"
book="$2"
printf "func=%s,book=%s\n" "$fun" "$book"
}
func2 () {
fun2="$1"
book2="$2"
printf "func2=%s,book2=%s\n" "... | <p>You can refactor your <code>second.sh</code> script like this:</p> <pre><code>func1 () { fun="$1" book="$2" printf "func=%s,book=%s\n" "$fun" "$book" } func2 () { fun2="$1" book2="$2" printf "func2=%s,book2=%s\n" "... | 34, 390 | shell, unix | <h1>Can I call a function of a shell script from another shell script?</h1>
<p>I have 2 shell scripts.</p>
<p>The second shell script contains following functions
<em>second.sh</em></p>
<pre><code>func1
func2
</code></pre>
<p>The first.sh will call the second shell script with some parameters and
will call func1 a... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 861 | bash | # Can I call a function of a shell script from another shell script?
I have 2 shell scripts.
The second shell script contains following functions
*second.sh*
```
func1
func2
```
The first.sh will call the second shell script with some parameters and
will call func1 and func2 with some other parameters specific to ... | You can refactor your `second.sh` script like this:
```
func1 () {
fun="$1"
book="$2"
printf "func=%s,book=%s\n" "$fun" "$book"
}
func2 () {
fun2="$1"
book2="$2"
printf "func2=%s,book2=%s\n" "$fun2" "$book2"
}
```
And then call these functions from script `first.sh` like this:
```
. ./second.sh
fu... |
13043344 | Search and replace in bash using regular expressions | 270 | 2012-10-24 05:10:06 | <p>I've seen this example:</p>
<pre><code>hello=ho02123ware38384you443d34o3434ingtod38384day
echo ${hello//[0-9]/}
</code></pre>
<p>Which follows this syntax: <code>${variable//pattern/replacement}</code></p>
<p>Unfortunately the <code>pattern</code> field doesn't seem to support full regex syntax (if I use <code>.<... | 389,284 | 1,232,685 | 2025-03-23 11:44:51 | 13,043,397 | 239 | 2012-10-24 05:16:53 | 197,772 | 2019-04-24 16:23:28 | https://stackoverflow.com/q/13043344 | https://stackoverflow.com/a/13043397 | <p>Use <a href="http://www.gnu.org/software/sed/manual/" rel="noreferrer">sed</a>:</p>
<pre><code>MYVAR=ho02123ware38384you443d34o3434ingtod38384day
echo "$MYVAR" | sed -e 's/[a-zA-Z]/X/g' -e 's/[0-9]/N/g'
# prints XXNNNNNXXXXNNNNNXXXNNNXNNXNNNNXXXXXXNNNNNXXX
</code></pre>
<p>Note that the subsequent <code>-e</code>'... | <p>Use <a href="http://www.gnu.org/software/sed/manual/" rel="noreferrer">sed</a>:</p> <pre><code>MYVAR=ho02123ware38384you443d34o3434ingtod38384day echo "$MYVAR" | sed -e 's/[a-zA-Z]/X/g' -e 's/[0-9]/N/g' # prints XXNNNNNXXXXNNNNNXXXNNNXNNXNNNNXXXXXXNNNNNXXX </code></pre> <p>Note that the subsequent <code>-e</code>'... | 18, 387 | bash, regex | <h1>Search and replace in bash using regular expressions</h1>
<p>I've seen this example:</p>
<pre><code>hello=ho02123ware38384you443d34o3434ingtod38384day
echo ${hello//[0-9]/}
</code></pre>
<p>Which follows this syntax: <code>${variable//pattern/replacement}</code></p>
<p>Unfortunately the <code>pattern</code> fiel... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 862 | bash | # Search and replace in bash using regular expressions
I've seen this example:
```
hello=ho02123ware38384you443d34o3434ingtod38384day
echo ${hello//[0-9]/}
```
Which follows this syntax: `${variable//pattern/replacement}`
Unfortunately the `pattern` field doesn't seem to support full regex syntax (if I use `.` or `... | Use [sed](http://www.gnu.org/software/sed/manual/):
```
MYVAR=ho02123ware38384you443d34o3434ingtod38384day
echo "$MYVAR" | sed -e 's/[a-zA-Z]/X/g' -e 's/[0-9]/N/g'
# prints XXNNNNNXXXXNNNNNXXXNNNXNNXNNNNXXXXXXNNNNNXXX
```
Note that the subsequent `-e`'s are processed in order. Also, the `g` flag for the expression wi... |
1063347 | Passing arrays as parameters in bash | 224 | 2009-06-30 12:21:40 | <p>How can I pass an array as parameter to a bash function?</p>
| 197,451 | 60,281 | 2023-05-15 09:12:58 | 4,017,175 | 239 | 2010-10-25 17:24:53 | 475,960 | 2019-10-25 11:26:54 | https://stackoverflow.com/q/1063347 | https://stackoverflow.com/a/4017175 | <p>You can pass <strong>multiple arrays as arguments</strong> using something like this:</p>
<pre><code>takes_ary_as_arg()
{
declare -a argAry1=("${!1}")
echo "${argAry1[@]}"
declare -a argAry2=("${!2}")
echo "${argAry2[@]}"
}
try_with_local_arys()
{
# array variables could have local scope
lo... | <p>You can pass <strong>multiple arrays as arguments</strong> using something like this:</p> <pre><code>takes_ary_as_arg() { declare -a argAry1=("${!1}") echo "${argAry1[@]}" declare -a argAry2=("${!2}") echo "${argAry2[@]}" } try_with_local_arys() { # array variables could have local scope lo... | 114, 387 | arrays, bash | <h1>Passing arrays as parameters in bash</h1>
<p>How can I pass an array as parameter to a bash function?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 863 | bash | # Passing arrays as parameters in bash
How can I pass an array as parameter to a bash function? | You can pass **multiple arrays as arguments** using something like this:
```
takes_ary_as_arg()
{
declare -a argAry1=("${!1}")
echo "${argAry1[@]}"
declare -a argAry2=("${!2}")
echo "${argAry2[@]}"
}
try_with_local_arys()
{
# array variables could have local scope
local descTable=(
"sl... |
414164 | How can I select random files from a directory in bash? | 212 | 2009-01-05 19:15:35 | <p>I have a directory with about 2000 files. How can I select a random sample of <code>N</code> files through using either a bash script or a list of piped commands?</p>
| 167,432 | 8,330 | 2022-03-04 00:13:08 | 414,316 | 239 | 2009-01-05 20:01:13 | 19,750 | 2009-01-05 20:01:13 | https://stackoverflow.com/q/414164 | https://stackoverflow.com/a/414316 | <p>Here's a script that uses GNU sort's random option:</p>
<pre><code>ls |sort -R |tail -$N |while read file; do
# Something involving $file, or you can leave
# off the while to just get the filenames
done
</code></pre>
| <p>Here's a script that uses GNU sort's random option:</p> <pre><code>ls |sort -R |tail -$N |while read file; do # Something involving $file, or you can leave # off the while to just get the filenames done </code></pre> | 197, 387 | bash, random | <h1>How can I select random files from a directory in bash?</h1>
<p>I have a directory with about 2000 files. How can I select a random sample of <code>N</code> files through using either a bash script or a list of piped commands?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 864 | bash | # How can I select random files from a directory in bash?
I have a directory with about 2000 files. How can I select a random sample of `N` files through using either a bash script or a list of piped commands? | Here's a script that uses GNU sort's random option:
```
ls |sort -R |tail -$N |while read file; do
# Something involving $file, or you can leave
# off the while to just get the filenames
done
``` |
14964805 | Groups of compound conditions in Bash test | 115 | 2013-02-19 18:30:50 | <p>I want to have some groups of conditions in a Bash <em>if</em> statement. Specifically, I'm looking for something like the following:</p>
<pre><code>if <myCondition1 and myCondition2> or <myCondition3 and myCondition4> then...
</code></pre>
<p>How may I group the conditions together in the way I describe... | 119,516 | 1,556,092 | 2025-01-15 00:46:57 | 14,965,071 | 239 | 2013-02-19 18:47:23 | 2,005,672 | 2025-01-15 00:46:57 | https://stackoverflow.com/q/14964805 | https://stackoverflow.com/a/14965071 | <p>Use the && (and) and || (or) operators:</p>
<pre><code>if [[ expression ]] && [[ expression ]] || [[ expression ]] ; then
</code></pre>
<p>They can also be used within a single [[ ]]:</p>
<pre><code>if [[ expression && expression || expression ]] ; then
</code></pre>
<p>And, finally, you can ... | <p>Use the && (and) and || (or) operators:</p> <pre><code>if [[ expression ]] && [[ expression ]] || [[ expression ]] ; then </code></pre> <p>They can also be used within a single [[ ]]:</p> <pre><code>if [[ expression && expression || expression ]] ; then </code></pre> <p>And, finally, you can ... | 387, 2773 | bash, if-statement | <h1>Groups of compound conditions in Bash test</h1>
<p>I want to have some groups of conditions in a Bash <em>if</em> statement. Specifically, I'm looking for something like the following:</p>
<pre><code>if <myCondition1 and myCondition2> or <myCondition3 and myCondition4> then...
</code></pre>
<p>How may I... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 865 | bash | # Groups of compound conditions in Bash test
I want to have some groups of conditions in a Bash *if* statement. Specifically, I'm looking for something like the following:
```
if <myCondition1 and myCondition2> or <myCondition3 and myCondition4> then...
```
How may I group the conditions together in the way I descri... | Use the && (and) and || (or) operators:
```
if [[ expression ]] && [[ expression ]] || [[ expression ]] ; then
```
They can also be used within a single [[ ]]:
```
if [[ expression && expression || expression ]] ; then
```
And, finally, you can group them to ensure order of evaluation:
```
if [[ expression && ( ex... |
23843106 | How to set child process' environment variable in Makefile | 232 | 2014-05-24 08:52:52 | <p>I would like to change this Makefile:</p>
<pre><code>SHELL := /bin/bash
PATH := node_modules/.bin:$(PATH)
boot:
@supervisor \
--harmony \
--watch etc,lib \
--extensions js,json \
--no-restart-on error \
lib
test:
NODE_ENV=test mocha \... | 355,884 | 1,246,987 | 2020-06-03 17:16:04 | 23,845,391 | 238 | 2014-05-24 13:06:01 | 939,557 | 2020-02-02 22:56:05 | https://stackoverflow.com/q/23843106 | https://stackoverflow.com/a/23845391 | <p>Make variables are not exported into the environment of processes make invokes... by default. However you can use make's <code>export</code> to force them to do so. Change:</p>
<pre><code>test: NODE_ENV = test
</code></pre>
<p>to this:</p>
<pre><code>test: export NODE_ENV = test
</code></pre>
<p>(assuming you ... | <p>Make variables are not exported into the environment of processes make invokes... by default. However you can use make's <code>export</code> to force them to do so. Change:</p> <pre><code>test: NODE_ENV = test </code></pre> <p>to this:</p> <pre><code>test: export NODE_ENV = test </code></pre> <p>(assuming you ... | 390, 4301, 9013, 17696 | environment-variables, makefile, shell, target | <h1>How to set child process' environment variable in Makefile</h1>
<p>I would like to change this Makefile:</p>
<pre><code>SHELL := /bin/bash
PATH := node_modules/.bin:$(PATH)
boot:
@supervisor \
--harmony \
--watch etc,lib \
--extensions js,json \
--no-restart... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 866 | bash | # How to set child process' environment variable in Makefile
I would like to change this Makefile:
```
SHELL := /bin/bash
PATH := node_modules/.bin:$(PATH)
boot:
@supervisor \
--harmony \
--watch etc,lib \
--extensions js,json \
--no-restart-on error \
... | Make variables are not exported into the environment of processes make invokes... by default. However you can use make's `export` to force them to do so. Change:
```
test: NODE_ENV = test
```
to this:
```
test: export NODE_ENV = test
```
(assuming you have a sufficiently modern version of GNU make >= 3.77 ). |
38576337 | How to execute a Bash command only if a Docker container with a given name does not exist? | 146 | 2016-07-25 19:56:40 | <p>On a Jenkins machine I would like to create a docker container with a specified name only if it does not already exist (in a shell script). I thought I might run the command to create the container regardless and ignore the failure if there was one, but this causes my jenkins job to fail.</p>
<p>Hence, I would like... | 177,950 | 2,570,017 | 2024-05-31 01:33:13 | 38,576,401 | 238 | 2016-07-25 20:00:23 | 61,903 | 2022-12-27 16:20:09 | https://stackoverflow.com/q/38576337 | https://stackoverflow.com/a/38576401 | <p>You can check for non-existence of a running container by grepping for a <code><name></code> and fire it up later on like this:</p>
<pre><code>[ ! "$(docker ps -a | grep <name>)" ] && docker run -d --name <name> <image>
</code></pre>
<p>Better:</p>
<p>Make use of <a href="ht... | <p>You can check for non-existence of a running container by grepping for a <code><name></code> and fire it up later on like this:</p> <pre><code>[ ! "$(docker ps -a | grep <name>)" ] && docker run -d --name <name> <image> </code></pre> <p>Better:</p> <p>Make use of <a href="ht... | 18, 387, 90304 | bash, docker, regex | <h1>How to execute a Bash command only if a Docker container with a given name does not exist?</h1>
<p>On a Jenkins machine I would like to create a docker container with a specified name only if it does not already exist (in a shell script). I thought I might run the command to create the container regardless and igno... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 867 | bash | # How to execute a Bash command only if a Docker container with a given name does not exist?
On a Jenkins machine I would like to create a docker container with a specified name only if it does not already exist (in a shell script). I thought I might run the command to create the container regardless and ignore the fa... | You can check for non-existence of a running container by grepping for a `<name>` and fire it up later on like this:
```
[ ! "$(docker ps -a | grep <name>)" ] && docker run -d --name <name> <image>
```
Better:
Make use of <https://docs.docker.com/engine/reference/commandline/ps/> and check if an exited container blo... |
246725 | How do I add tab completion to the Python shell? | 146 | 2008-10-29 13:09:10 | <p>When starting a django application using <code>python manage.py shell</code>, I get an InteractiveConsole shell - I can use tab completion, etc.</p>
<pre><code>Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more in... | 109,915 | 22,306 | 2021-10-07 23:16:00 | 246,779 | 238 | 2008-10-29 13:24:39 | 22,306 | 2014-03-21 12:15:37 | https://stackoverflow.com/q/246725 | https://stackoverflow.com/a/246779 | <p>I may have found a way to do it.</p>
<p>Create a file .pythonrc</p>
<pre><code># ~/.pythonrc
# enable syntax completion
try:
import readline
except ImportError:
print("Module readline not available.")
else:
import rlcompleter
readline.parse_and_bind("tab: complete")
</code></pre>
<p>then in your .... | <p>I may have found a way to do it.</p> <p>Create a file .pythonrc</p> <pre><code># ~/.pythonrc # enable syntax completion try: import readline except ImportError: print("Module readline not available.") else: import rlcompleter readline.parse_and_bind("tab: complete") </code></pre> <p>then in your .... | 16, 390, 1687 | interpreter, python, shell | <h1>How do I add tab completion to the Python shell?</h1>
<p>When starting a django application using <code>python manage.py shell</code>, I get an InteractiveConsole shell - I can use tab completion, etc.</p>
<pre><code>Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Ty... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 868 | bash | # How do I add tab completion to the Python shell?
When starting a django application using `python manage.py shell`, I get an InteractiveConsole shell - I can use tab completion, etc.
```
Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credit... | I may have found a way to do it.
Create a file .pythonrc
```
# ~/.pythonrc
# enable syntax completion
try:
import readline
except ImportError:
print("Module readline not available.")
else:
import rlcompleter
readline.parse_and_bind("tab: complete")
```
then in your .bashrc file, add
```
export PYTHO... |
28330907 | How to use AWS S3 CLI to dump files to stdout in BASH? | 144 | 2015-02-04 20:33:44 | <p>I'm starting a bash script which will take a path in S3 (as specified to the <a href="http://docs.aws.amazon.com/cli/latest/reference/s3/ls.html" rel="noreferrer">ls</a> command) and dump the contents of all of the file objects to <code>stdout</code>. Essentially I'd like to replicate <code>cat /path/to/files/*</cod... | 95,343 | 9,642 | 2020-11-30 19:25:09 | 28,390,423 | 238 | 2015-02-08 04:02:24 | 2,239,016 | 2015-02-08 04:02:24 | https://stackoverflow.com/q/28330907 | https://stackoverflow.com/a/28390423 | <blockquote>
<p>dump the contents of all of the file objects to stdout.</p>
</blockquote>
<p>You can accomplish this if you pass <code>-</code> for destination of <code>aws s3 cp</code> command.
For example, <code>$ aws s3 cp s3://mybucket/stream.txt -</code>.</p>
<p>What you're trying to do is something like thi... | <blockquote> <p>dump the contents of all of the file objects to stdout.</p> </blockquote> <p>You can accomplish this if you pass <code>-</code> for destination of <code>aws s3 cp</code> command. For example, <code>$ aws s3 cp s3://mybucket/stream.txt -</code>.</p> <p>What you're trying to do is something like thi... | 387, 11444, 33388, 99166 | amazon-s3, amazon-web-services, aws-cli, bash | <h1>How to use AWS S3 CLI to dump files to stdout in BASH?</h1>
<p>I'm starting a bash script which will take a path in S3 (as specified to the <a href="http://docs.aws.amazon.com/cli/latest/reference/s3/ls.html" rel="noreferrer">ls</a> command) and dump the contents of all of the file objects to <code>stdout</code>. E... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 869 | bash | # How to use AWS S3 CLI to dump files to stdout in BASH?
I'm starting a bash script which will take a path in S3 (as specified to the [ls](http://docs.aws.amazon.com/cli/latest/reference/s3/ls.html) command) and dump the contents of all of the file objects to `stdout`. Essentially I'd like to replicate `cat /path/to/f... | > dump the contents of all of the file objects to stdout.
You can accomplish this if you pass `-` for destination of `aws s3 cp` command.
For example, `$ aws s3 cp s3://mybucket/stream.txt -`.
What you're trying to do is something like this? ::
```
#!/bin/bash
BUCKET=YOUR-BUCKET-NAME
for key in `aws s3api list-obje... |
33707193 | How to convert string to integer in PowerShell | 125 | 2015-11-14 10:00:37 | <p>I have a list of directories with numbers. I have to find the highest number and and increment it by 1 and create a new directory with that increment value. I am able to sort the below array, but I am not able to increment the last element as it is a string.</p>
<p>How do I convert this below array element to an in... | 462,777 | 5,553,314 | 2024-01-30 18:20:47 | 33,707,377 | 238 | 2015-11-14 10:21:48 | 3,677,139 | 2019-06-11 16:22:49 | https://stackoverflow.com/q/33707193 | https://stackoverflow.com/a/33707377 | <p>You can specify the type of a variable before it to force its type. It's called <strong>(dynamic) casting</strong> (<a href="https://ss64.com/ps/syntax-datatypes.html" rel="noreferrer">more information is here</a>):</p>
<pre><code>$string = "1654"
$integer = [int]$string
$string + 1
# Outputs 16541
$integer + 1
#... | <p>You can specify the type of a variable before it to force its type. It's called <strong>(dynamic) casting</strong> (<a href="https://ss64.com/ps/syntax-datatypes.html" rel="noreferrer">more information is here</a>):</p> <pre><code>$string = "1654" $integer = [int]$string $string + 1 # Outputs 16541 $integer + 1 #... | 526 | powershell | <h1>How to convert string to integer in PowerShell</h1>
<p>I have a list of directories with numbers. I have to find the highest number and and increment it by 1 and create a new directory with that increment value. I am able to sort the below array, but I am not able to increment the last element as it is a string.</p... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 870 | bash | # How to convert string to integer in PowerShell
I have a list of directories with numbers. I have to find the highest number and and increment it by 1 and create a new directory with that increment value. I am able to sort the below array, but I am not able to increment the last element as it is a string.
How do I c... | You can specify the type of a variable before it to force its type. It's called **(dynamic) casting** ([more information is here](https://ss64.com/ps/syntax-datatypes.html)):
```
$string = "1654"
$integer = [int]$string
$string + 1
# Outputs 16541
$integer + 1
# Outputs 1655
```
---
As an example, the following sn... |
7285059 | HMAC-SHA1 in bash | 122 | 2011-09-02 14:36:52 | <p>Is there a bash script to generate a <code>HMAC-SHA1</code> hash?</p>
<p>I'm looking for something equivalent to the following PHP code:</p>
<pre><code>hash_hmac("sha1", "value", "key");
</code></pre>
| 116,425 | 298,406 | 2020-10-09 14:01:52 | 7,285,256 | 238 | 2011-09-02 14:52:23 | 115,845 | 2017-02-14 19:38:07 | https://stackoverflow.com/q/7285059 | https://stackoverflow.com/a/7285256 | <p>I realise this isn't exactly what you're asking for, but there's no point in reinventing the wheel and writing a bash version.</p>
<p>You can simply use the <a href="https://www.openssl.org/docs/man1.1.0/apps/dgst.html" rel="noreferrer"><code>openssl</code></a> command to generate the hash within your script.</p>
... | <p>I realise this isn't exactly what you're asking for, but there's no point in reinventing the wheel and writing a bash version.</p> <p>You can simply use the <a href="https://www.openssl.org/docs/man1.1.0/apps/dgst.html" rel="noreferrer"><code>openssl</code></a> command to generate the hash within your script.</p> ... | 387, 3190, 19452 | bash, hmac, sha1 | <h1>HMAC-SHA1 in bash</h1>
<p>Is there a bash script to generate a <code>HMAC-SHA1</code> hash?</p>
<p>I'm looking for something equivalent to the following PHP code:</p>
<pre><code>hash_hmac("sha1", "value", "key");
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 871 | bash | # HMAC-SHA1 in bash
Is there a bash script to generate a `HMAC-SHA1` hash?
I'm looking for something equivalent to the following PHP code:
```
hash_hmac("sha1", "value", "key");
``` | I realise this isn't exactly what you're asking for, but there's no point in reinventing the wheel and writing a bash version.
You can simply use the [`openssl`](https://www.openssl.org/docs/man1.1.0/apps/dgst.html) command to generate the hash within your script.
```
[me@home] echo -n "value" | openssl dgst -sha1 -h... |
9605232 | How to merge every two lines into one from the command line? | 227 | 2012-03-07 16:19:56 | <p>I have a text file with the following format. The first line is the "KEY" and the second line is the "VALUE". </p>
<pre><code>KEY 4048:1736 string
3
KEY 0:1772 string
1
KEY 4192:1349 string
1
KEY 7329:2407 string
2
KEY 0:1774 string
1
</code></pre>
<p>I need the value in the same line as of the key. So the output ... | 339,972 | 139,150 | 2024-08-07 08:55:41 | 9,605,559 | 237 | 2012-03-07 16:39:26 | 164,835 | 2016-07-04 08:17:13 | https://stackoverflow.com/q/9605232 | https://stackoverflow.com/a/9605559 | <p>awk:</p>
<pre><code>awk 'NR%2{printf "%s ",$0;next;}1' yourFile
</code></pre>
<p>note, there is an empty line at the end of output.</p>
<p>sed:</p>
<pre><code>sed 'N;s/\n/ /' yourFile
</code></pre>
| <p>awk:</p> <pre><code>awk 'NR%2{printf "%s ",$0;next;}1' yourFile </code></pre> <p>note, there is an empty line at the end of output.</p> <p>sed:</p> <pre><code>sed 'N;s/\n/ /' yourFile </code></pre> | 387, 717, 990, 1271, 5282 | awk, bash, grep, merge, sed | <h1>How to merge every two lines into one from the command line?</h1>
<p>I have a text file with the following format. The first line is the "KEY" and the second line is the "VALUE". </p>
<pre><code>KEY 4048:1736 string
3
KEY 0:1772 string
1
KEY 4192:1349 string
1
KEY 7329:2407 string
2
KEY 0:1774 string
1
</code></pr... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 872 | bash | # How to merge every two lines into one from the command line?
I have a text file with the following format. The first line is the "KEY" and the second line is the "VALUE".
```
KEY 4048:1736 string
3
KEY 0:1772 string
1
KEY 4192:1349 string
1
KEY 7329:2407 string
2
KEY 0:1774 string
1
```
I need the value in the sam... | awk:
```
awk 'NR%2{printf "%s ",$0;next;}1' yourFile
```
note, there is an empty line at the end of output.
sed:
```
sed 'N;s/\n/ /' yourFile
``` |
73833 | How do you search for files containing DOS line endings (CRLF) with grep on Linux? | 178 | 2008-09-16 15:50:58 | <p>I want to search for files containing DOS line endings with grep on Linux. Something like this:</p>
<pre class="lang-sh prettyprint-override"><code>grep -IUr --color '\r\n' .
</code></pre>
<p>The above seems to match for literal <code>rn</code> which is not what is desired.</p>
<p>The output of this will be piped t... | 130,629 | 10,245 | 2024-09-18 09:46:17 | 73,969 | 237 | 2008-09-16 16:03:28 | 12,825 | 2008-09-16 16:03:28 | https://stackoverflow.com/q/73833 | https://stackoverflow.com/a/73969 | <p>grep probably isn't the tool you want for this. It will print a line for every matching line in every file. Unless you want to, say, run todos 10 times on a 10 line file, grep isn't the best way to go about it. Using find to run file on every file in the tree then grepping through that for "CRLF" will get you one... | <p>grep probably isn't the tool you want for this. It will print a line for every matching line in every file. Unless you want to, say, run todos 10 times on a 10 line file, grep isn't the best way to go about it. Using find to run file on every file in the tree then grepping through that for "CRLF" will get you one... | 58, 387, 1271, 3705 | bash, grep, linux, newline | <h1>How do you search for files containing DOS line endings (CRLF) with grep on Linux?</h1>
<p>I want to search for files containing DOS line endings with grep on Linux. Something like this:</p>
<pre class="lang-sh prettyprint-override"><code>grep -IUr --color '\r\n' .
</code></pre>
<p>The above seems to match for lit... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 873 | bash | # How do you search for files containing DOS line endings (CRLF) with grep on Linux?
I want to search for files containing DOS line endings with grep on Linux. Something like this:
```
grep -IUr --color '\r\n' .
```
The above seems to match for literal `rn` which is not what is desired.
The output of this will be p... | grep probably isn't the tool you want for this. It will print a line for every matching line in every file. Unless you want to, say, run todos 10 times on a 10 line file, grep isn't the best way to go about it. Using find to run file on every file in the tree then grepping through that for "CRLF" will get you one line ... |
3742983 | How to get the contents of a webpage in a shell variable? | 138 | 2010-09-18 18:44:42 | <p>In Linux how can I fetch an URL and get its contents in a variable in shell script? </p>
| 391,034 | 414,064 | 2024-12-05 13:27:24 | 3,742,990 | 237 | 2010-09-18 18:46:19 | 227,665 | 2010-09-23 13:06:10 | https://stackoverflow.com/q/3742983 | https://stackoverflow.com/a/3742990 | <p>You can use <code>wget</code> command to download the page and read it into a variable as:</p>
<pre><code>content=$(wget google.com -q -O -)
echo $content
</code></pre>
<p>We use the <code>-O</code> option of <code>wget</code> which allows us to specify the name of the file into which <code>wget</code> dumps the p... | <p>You can use <code>wget</code> command to download the page and read it into a variable as:</p> <pre><code>content=$(wget google.com -q -O -) echo $content </code></pre> <p>We use the <code>-O</code> option of <code>wget</code> which allows us to specify the name of the file into which <code>wget</code> dumps the p... | 58, 387, 390, 5583 | bash, linux, shell, wget | <h1>How to get the contents of a webpage in a shell variable?</h1>
<p>In Linux how can I fetch an URL and get its contents in a variable in 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 | 874 | bash | # How to get the contents of a webpage in a shell variable?
In Linux how can I fetch an URL and get its contents in a variable in shell script? | You can use `wget` command to download the page and read it into a variable as:
```
content=$(wget google.com -q -O -)
echo $content
```
We use the `-O` option of `wget` which allows us to specify the name of the file into which `wget` dumps the page contents. We specify `-` to get the dump onto standard output and c... |
1405324 | How to create a bash script to check the SSH connection? | 135 | 2009-09-10 13:30:04 | <p>I am in the process of creating a bash script that would log into the remote machines and create private and public keys.</p>
<p>My problem is that the remote machines are not very reliable, and they are not always up. I need a bash script that would check if the SSH connection is up. Before actually creating the k... | 241,912 | 154,688 | 2023-11-14 21:33:18 | 1,405,340 | 237 | 2009-09-10 13:33:40 | null | 2023-11-14 21:33:18 | https://stackoverflow.com/q/1405324 | https://stackoverflow.com/a/1405340 | <p>You can check this with the return-value ssh gives you:</p>
<pre><code>$ ssh -q user@downhost exit
$ echo $?
255
$ ssh -q user@uphost exit
$ echo $?
0
</code></pre>
<p>EDIT: Another approach would be to use nmap (you won't need to have keys or login-stuff):</p>
<pre><code>$ a=`nmap uphost -PN -p ssh | grep open`
$ ... | <p>You can check this with the return-value ssh gives you:</p> <pre><code>$ ssh -q user@downhost exit $ echo $? 255 $ ssh -q user@uphost exit $ echo $? 0 </code></pre> <p>EDIT: Another approach would be to use nmap (you won't need to have keys or login-stuff):</p> <pre><code>$ a=`nmap uphost -PN -p ssh | grep open` $ ... | 281, 386, 387 | bash, connection, ssh | <h1>How to create a bash script to check the SSH connection?</h1>
<p>I am in the process of creating a bash script that would log into the remote machines and create private and public keys.</p>
<p>My problem is that the remote machines are not very reliable, and they are not always up. I need a bash script that would... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 875 | bash | # How to create a bash script to check the SSH connection?
I am in the process of creating a bash script that would log into the remote machines and create private and public keys.
My problem is that the remote machines are not very reliable, and they are not always up. I need a bash script that would check if the SS... | You can check this with the return-value ssh gives you:
```
$ ssh -q user@downhost exit
$ echo $?
255
$ ssh -q user@uphost exit
$ echo $?
0
```
EDIT: Another approach would be to use nmap (you won't need to have keys or login-stuff):
```
$ a=`nmap uphost -PN -p ssh | grep open`
$ b=`nmap downhost -PN -p ssh | grep ... |
15120597 | Passing multiple values to a single PowerShell script parameter | 131 | 2013-02-27 19:24:10 | <p>I have a script to which I pass server name(s) in $args.</p>
<p>This way I can do stuff to this (these) server(s) using <code>foreach</code>:</p>
<pre><code>.\script.ps1 host1 host2 host3
foreach ($i in $args)
{
Do-Stuff $i
}
</code></pre>
<p>I'd like to add a named optional parameter called vlan. I've tried... | 373,566 | 1,483,127 | 2022-02-04 13:22:10 | 15,120,707 | 237 | 2013-02-27 19:30:23 | 2,102,693 | 2019-01-02 15:30:50 | https://stackoverflow.com/q/15120597 | https://stackoverflow.com/a/15120707 | <p>The easiest way is probably to use two parameters: One for hosts (can be an array), and one for vlan.</p>
<pre><code>param([String[]] $Hosts, [String] $VLAN)
</code></pre>
<p>Instead of</p>
<pre><code>foreach ($i in $args)
</code></pre>
<p>you can use</p>
<pre><code>foreach ($hostName in $Hosts)
</code></pre>
... | <p>The easiest way is probably to use two parameters: One for hosts (can be an array), and one for vlan.</p> <pre><code>param([String[]] $Hosts, [String] $VLAN) </code></pre> <p>Instead of</p> <pre><code>foreach ($i in $args) </code></pre> <p>you can use</p> <pre><code>foreach ($hostName in $Hosts) </code></pre> ... | 360, 526 | parameters, powershell | <h1>Passing multiple values to a single PowerShell script parameter</h1>
<p>I have a script to which I pass server name(s) in $args.</p>
<p>This way I can do stuff to this (these) server(s) using <code>foreach</code>:</p>
<pre><code>.\script.ps1 host1 host2 host3
foreach ($i in $args)
{
Do-Stuff $i
}
</code></pr... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 876 | bash | # Passing multiple values to a single PowerShell script parameter
I have a script to which I pass server name(s) in $args.
This way I can do stuff to this (these) server(s) using `foreach`:
```
.\script.ps1 host1 host2 host3
foreach ($i in $args)
{
Do-Stuff $i
}
```
I'd like to add a named optional parameter c... | The easiest way is probably to use two parameters: One for hosts (can be an array), and one for vlan.
```
param([String[]] $Hosts, [String] $VLAN)
```
Instead of
```
foreach ($i in $args)
```
you can use
```
foreach ($hostName in $Hosts)
```
If there is only one host, the foreach loop will iterate only once. To p... |
602706 | Batch renaming files with Bash | 128 | 2009-03-02 15:20:44 | <p>How can Bash rename a series of packages to remove their version numbers? I've been toying around with both <code>expr</code> and <code>%%</code>, to no avail.</p>
<p>Examples:</p>
<p><code>Xft2-2.1.13.pkg</code> becomes <code>Xft2.pkg</code></p>
<p><code>jasper-1.900.1.pkg</code> becomes <code>jasper.pkg</code><... | 129,411 | 70,519 | 2023-12-08 10:47:03 | 602,770 | 237 | 2009-03-02 15:33:46 | 4,596 | 2018-03-04 19:31:17 | https://stackoverflow.com/q/602706 | https://stackoverflow.com/a/602770 | <p>You could use bash's parameter expansion feature</p>
<pre><code>for i in ./*.pkg ; do mv "$i" "${i/-[0-9.]*.pkg/.pkg}" ; done
</code></pre>
<p>Quotes are needed for filenames with spaces.</p>
| <p>You could use bash's parameter expansion feature</p> <pre><code>for i in ./*.pkg ; do mv "$i" "${i/-[0-9.]*.pkg/.pkg}" ; done </code></pre> <p>Quotes are needed for filenames with spaces.</p> | 387, 390, 12895 | bash, file-rename, shell | <h1>Batch renaming files with Bash</h1>
<p>How can Bash rename a series of packages to remove their version numbers? I've been toying around with both <code>expr</code> and <code>%%</code>, to no avail.</p>
<p>Examples:</p>
<p><code>Xft2-2.1.13.pkg</code> becomes <code>Xft2.pkg</code></p>
<p><code>jasper-1.900.1.pkg... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 877 | bash | # Batch renaming files with Bash
How can Bash rename a series of packages to remove their version numbers? I've been toying around with both `expr` and `%%`, to no avail.
Examples:
`Xft2-2.1.13.pkg` becomes `Xft2.pkg`
`jasper-1.900.1.pkg` becomes `jasper.pkg`
`xorg-libXrandr-1.2.3.pkg` becomes `xorg-libXrandr.pkg` | You could use bash's parameter expansion feature
```
for i in ./*.pkg ; do mv "$i" "${i/-[0-9.]*.pkg/.pkg}" ; done
```
Quotes are needed for filenames with spaces. |
16778667 | How to use SED to find and replace URL strings with the "/" character in the targeted strings? | 83 | 2013-05-27 18:28:34 | <p>I'm attempting to use SED through OS X Terminal to perform a find and replace.</p>
<p>Imagine I have this string littered throughout the text file: <a href="http://www.find.com/page" rel="noreferrer">http://www.find.com/page</a></p>
<p>And I want to replace it with this string: <a href="http://www.replace.com/page... | 115,212 | 455,810 | 2013-05-27 18:43:29 | 16,778,711 | 237 | 2013-05-27 18:31:56 | 477,878 | 2013-05-27 18:31:56 | https://stackoverflow.com/q/16778667 | https://stackoverflow.com/a/16778711 | <p><code>/</code> is not <em>the</em> delimiter in <code>sed</code> commands, it's just one of the possible ones. For this example, you can for example use <code>,</code> instead since it does not conflict with your strings;</p>
<pre><code>echo 'I think http://www.find.com/page is my favorite' |
sed 's,http://www... | <p><code>/</code> is not <em>the</em> delimiter in <code>sed</code> commands, it's just one of the possible ones. For this example, you can for example use <code>,</code> instead since it does not conflict with your strings;</p> <pre><code>echo 'I think http://www.find.com/page is my favorite' | sed 's,http://www... | 387, 4804, 5282 | bash, escaping, sed | <h1>How to use SED to find and replace URL strings with the "/" character in the targeted strings?</h1>
<p>I'm attempting to use SED through OS X Terminal to perform a find and replace.</p>
<p>Imagine I have this string littered throughout the text file: <a href="http://www.find.com/page" rel="noreferrer">http://www.f... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 878 | bash | # How to use SED to find and replace URL strings with the "/" character in the targeted strings?
I'm attempting to use SED through OS X Terminal to perform a find and replace.
Imagine I have this string littered throughout the text file: <http://www.find.com/page>
And I want to replace it with this string: <http://w... | `/` is not *the* delimiter in `sed` commands, it's just one of the possible ones. For this example, you can for example use `,` instead since it does not conflict with your strings;
```
echo 'I think http://www.find.com/page is my favorite' |
sed 's,http://www.find.com/page,http://www.replace.com/page,g'
``` |
10581206 | Boolean literals in PowerShell | 212 | 2012-05-14 10:05:33 | <p>What are the Boolean literals in PowerShell?</p>
| 141,267 | 284,795 | 2023-12-02 19:01:51 | 10,581,229 | 236 | 2012-05-14 10:06:51 | 73,070 | 2021-04-24 21:24:28 | https://stackoverflow.com/q/10581206 | https://stackoverflow.com/a/10581229 | <p><code>$true</code> and <code>$false</code>.</p>
<p>Those are constants, though. There are no language-level literals for Booleans.</p>
<p>Depending on where you need them, you can also use anything that <em>coerces</em> to a Boolean value, if the type has to be Boolean, e.g., in method calls that require Boolean (an... | <p><code>$true</code> and <code>$false</code>.</p> <p>Those are constants, though. There are no language-level literals for Booleans.</p> <p>Depending on where you need them, you can also use anything that <em>coerces</em> to a Boolean value, if the type has to be Boolean, e.g., in method calls that require Boolean (an... | 526, 2848, 5223 | boolean, constants, powershell | <h1>Boolean literals in PowerShell</h1>
<p>What are the Boolean literals 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 | 879 | bash | # Boolean literals in PowerShell
What are the Boolean literals in PowerShell? | `$true` and `$false`.
Those are constants, though. There are no language-level literals for Booleans.
Depending on where you need them, you can also use anything that *coerces* to a Boolean value, if the type has to be Boolean, e.g., in method calls that require Boolean (and have no conflicting overload), or conditio... |
25880122 | How do I use Join-Path to combine more than two strings into a file path? | 183 | 2014-09-16 23:45:24 | <p>If I want to combine two strings into a file path, I use <a href="https://learn.microsoft.com/en-gb/powershell/module/microsoft.powershell.management/join-path?view=powershell-7.4" rel="noreferrer"><code>Join-Path</code></a> like this:</p>
<pre><code>$path = Join-Path C: "Program Files"
Write-Host $path
</... | 182,061 | 2,766,558 | 2025-10-01 08:52:25 | 25,893,843 | 236 | 2014-09-17 14:51:32 | 2,439,252 | 2025-05-26 18:24:53 | https://stackoverflow.com/q/25880122 | https://stackoverflow.com/a/25893843 | <p>You can use the .NET <a href="https://learn.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=netframework-4.8" rel="nofollow noreferrer">Path</a> class:</p>
<pre><code>[IO.Path]::Combine('C:\', 'Foo', 'Bar')
</code></pre>
<p>Note: this will not work for any values that begin with a leading backslash, like ... | <p>You can use the .NET <a href="https://learn.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=netframework-4.8" rel="nofollow noreferrer">Path</a> class:</p> <pre><code>[IO.Path]::Combine('C:\', 'Foo', 'Bar') </code></pre> <p>Note: this will not work for any values that begin with a leading backslash, like ... | 526 | powershell | <h1>How do I use Join-Path to combine more than two strings into a file path?</h1>
<p>If I want to combine two strings into a file path, I use <a href="https://learn.microsoft.com/en-gb/powershell/module/microsoft.powershell.management/join-path?view=powershell-7.4" rel="noreferrer"><code>Join-Path</code></a> like this... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 880 | bash | # How do I use Join-Path to combine more than two strings into a file path?
If I want to combine two strings into a file path, I use [`Join-Path`](https://learn.microsoft.com/en-gb/powershell/module/microsoft.powershell.management/join-path?view=powershell-7.4) like this:
```
$path = Join-Path C: "Program Files"
Writ... | You can use the .NET [Path](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=netframework-4.8) class:
```
[IO.Path]::Combine('C:\', 'Foo', 'Bar')
```
Note: this will not work for any values that begin with a leading backslash, like [UNC paths](https://learn.microsoft.com/en-us/dotnet/standard/... |
2427995 | Bash, no-arguments warning, and case decisions | 147 | 2010-03-11 19:26:13 | <p>I am learning Bash.</p>
<p>I would like to do a simple script that, when no arguments are given, shows some message. And when I give numbers as arguments, depending on the value, it does one thing or another.</p>
| 128,759 | 171,546 | 2024-07-17 20:09:11 | 2,428,006 | 236 | 2010-03-11 19:28:25 | 14,637 | 2013-10-11 22:13:19 | https://stackoverflow.com/q/2427995 | https://stackoverflow.com/a/2428006 | <pre><code>if [[ $# -eq 0 ]] ; then
echo 'some message'
exit 0
fi
case "$1" in
1) echo 'you gave 1' ;;
*) echo 'you gave something else' ;;
esac
</code></pre>
<p>The <a href="http://tldp.org/LDP/abs/html/" rel="noreferrer">Advanced Bash-Scripting Guide</a> is pretty good. In spite of its name, it does... | <pre><code>if [[ $# -eq 0 ]] ; then echo 'some message' exit 0 fi case "$1" in 1) echo 'you gave 1' ;; *) echo 'you gave something else' ;; esac </code></pre> <p>The <a href="http://tldp.org/LDP/abs/html/" rel="noreferrer">Advanced Bash-Scripting Guide</a> is pretty good. In spite of its name, it does... | 387 | bash | <h1>Bash, no-arguments warning, and case decisions</h1>
<p>I am learning Bash.</p>
<p>I would like to do a simple script that, when no arguments are given, shows some message. And when I give numbers as arguments, depending on the value, it does one thing or another.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 881 | bash | # Bash, no-arguments warning, and case decisions
I am learning Bash.
I would like to do a simple script that, when no arguments are given, shows some message. And when I give numbers as arguments, depending on the value, it does one thing or another. | ```
if [[ $# -eq 0 ]] ; then
echo 'some message'
exit 0
fi
case "$1" in
1) echo 'you gave 1' ;;
*) echo 'you gave something else' ;;
esac
```
The [Advanced Bash-Scripting Guide](http://tldp.org/LDP/abs/html/) is pretty good. In spite of its name, it does treat the basics. |
1644593 | Compare two folders which have many files inside contents | 131 | 2009-10-29 15:36:04 | <p>Have two folders with approx. 150 java property files.</p>
<p>In a shell script, how to compare both folders to see if there is any new property file in either of them and what are the differences between the property files.</p>
<p>The output should be in a report format.</p>
| 137,336 | 174,908 | 2021-07-30 00:19:28 | 1,644,641 | 236 | 2009-10-29 15:42:35 | 197,368 | 2019-04-18 06:00:05 | https://stackoverflow.com/q/1644593 | https://stackoverflow.com/a/1644641 | <p>To get summary of new/missing files, and which files differ:</p>
<pre><code>diff -arq folder1 folder2
</code></pre>
<p><code>a</code> treats all files as text, <code>r</code> recursively searched subdirectories, <code>q</code> reports 'briefly', only when files differ</p>
| <p>To get summary of new/missing files, and which files differ:</p> <pre><code>diff -arq folder1 folder2 </code></pre> <p><code>a</code> treats all files as text, <code>r</code> recursively searched subdirectories, <code>q</code> reports 'briefly', only when files differ</p> | 34, 390, 606 | diff, shell, unix | <h1>Compare two folders which have many files inside contents</h1>
<p>Have two folders with approx. 150 java property files.</p>
<p>In a shell script, how to compare both folders to see if there is any new property file in either of them and what are the differences between the property files.</p>
<p>The output shoul... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 882 | bash | # Compare two folders which have many files inside contents
Have two folders with approx. 150 java property files.
In a shell script, how to compare both folders to see if there is any new property file in either of them and what are the differences between the property files.
The output should be in a report format... | To get summary of new/missing files, and which files differ:
```
diff -arq folder1 folder2
```
`a` treats all files as text, `r` recursively searched subdirectories, `q` reports 'briefly', only when files differ |
14132210 | Use find command but exclude files in two directories | 120 | 2013-01-03 02:21:32 | <p>I want to find files that end with <code>_peaks.bed</code>, but exclude files in the <code>tmp</code> and <code>scripts</code> folders.</p>
<p>My command is like this:</p>
<pre><code> find . -type f \( -name "*_peaks.bed" ! -name "*tmp*" ! -name "*scripts*" \)
</code></pre>
<p>But it didn't work. The files in <co... | 167,168 | 1,272,683 | 2024-12-02 11:56:23 | 14,132,309 | 236 | 2013-01-03 02:34:07 | 1,741,864 | 2013-01-03 02:46:12 | https://stackoverflow.com/q/14132210 | https://stackoverflow.com/a/14132309 | <p>Here's how you can specify that with <code>find</code>:</p>
<pre><code>find . -type f -name "*_peaks.bed" ! -path "./tmp/*" ! -path "./scripts/*"
</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>find .</code> - Start find from current working directory (recursively by default)</li>
<li><code>-typ... | <p>Here's how you can specify that with <code>find</code>:</p> <pre><code>find . -type f -name "*_peaks.bed" ! -path "./tmp/*" ! -path "./scripts/*" </code></pre> <p><strong>Explanation:</strong></p> <ul> <li><code>find .</code> - Start find from current working directory (recursively by default)</li> <li><code>-typ... | 34, 58, 390, 10193 | find, linux, shell, unix | <h1>Use find command but exclude files in two directories</h1>
<p>I want to find files that end with <code>_peaks.bed</code>, but exclude files in the <code>tmp</code> and <code>scripts</code> folders.</p>
<p>My command is like this:</p>
<pre><code> find . -type f \( -name "*_peaks.bed" ! -name "*tmp*" ! -name "*scri... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 883 | bash | # Use find command but exclude files in two directories
I want to find files that end with `_peaks.bed`, but exclude files in the `tmp` and `scripts` folders.
My command is like this:
```
find . -type f \( -name "*_peaks.bed" ! -name "*tmp*" ! -name "*scripts*" \)
```
But it didn't work. The files in `tmp` and `sc... | Here's how you can specify that with `find`:
```
find . -type f -name "*_peaks.bed" ! -path "./tmp/*" ! -path "./scripts/*"
```
**Explanation:**
- `find .` - Start find from current working directory (recursively by default)
- `-type f` - Specify to `find` that you only want files in the results
- `-name "*_peaks.be... |
3659602 | Automating "enter" keypresses for bash script generating ssh keys | 113 | 2010-09-07 14:31:14 | <p>I would like to create script, which simply runs <code>ssh-keygen -t rsa</code>. But how to pass to it 3 times enter?</p>
| 83,151 | 190,446 | 2020-02-02 07:10:57 | 3,659,691 | 236 | 2010-09-07 14:42:06 | 75,699 | 2010-09-08 13:23:37 | https://stackoverflow.com/q/3659602 | https://stackoverflow.com/a/3659691 | <p>Try:</p>
<pre><code>ssh-keygen -t rsa -N "" -f my.key
</code></pre>
<p><code>-N ""</code> tells it to use an empty passphrase (the same as two of the enters in an interactive script)</p>
<p><code>-f my.key</code> tells it to store the key into <code>my.key</code> (change as you see fit).</p>
<p>The whole thing r... | <p>Try:</p> <pre><code>ssh-keygen -t rsa -N "" -f my.key </code></pre> <p><code>-N ""</code> tells it to use an empty passphrase (the same as two of the enters in an interactive script)</p> <p><code>-f my.key</code> tells it to store the key into <code>my.key</code> (change as you see fit).</p> <p>The whole thing r... | 387 | bash | <h1>Automating "enter" keypresses for bash script generating ssh keys</h1>
<p>I would like to create script, which simply runs <code>ssh-keygen -t rsa</code>. But how to pass to it 3 times enter?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 884 | bash | # Automating "enter" keypresses for bash script generating ssh keys
I would like to create script, which simply runs `ssh-keygen -t rsa`. But how to pass to it 3 times enter? | Try:
```
ssh-keygen -t rsa -N "" -f my.key
```
`-N ""` tells it to use an empty passphrase (the same as two of the enters in an interactive script)
`-f my.key` tells it to store the key into `my.key` (change as you see fit).
The whole thing runs without you needing to supply any enter keys :)
To send enters to an ... |
9294949 | When should I use Write-Error vs. Throw? Terminating vs. non-terminating errors | 192 | 2012-02-15 14:13:36 | <p>Looking at a Get-WebFile script over on PoshCode, <a href="http://poshcode.org/3226" rel="noreferrer">http://poshcode.org/3226</a>, I noticed this strange-to-me contraption:</p>
<pre><code>$URL_Format_Error = [string]"..."
Write-Error $URL_Format_Error
return
</code></pre>
<p>What is the reason for this as opposed... | 227,796 | 108,993 | 2023-04-18 17:52:19 | 9,295,344 | 235 | 2012-02-15 14:38:49 | 251,123 | 2018-11-07 20:33:53 | https://stackoverflow.com/q/9294949 | https://stackoverflow.com/a/9295344 | <p><code>Write-Error</code> should be used if you want to inform the user of a non-critical error. By default all it does is print an error message in red text on the console. It does not stop a pipeline or a loop from continuing. <code>Throw</code> on the other hand produces what is called a terminating error. If you ... | <p><code>Write-Error</code> should be used if you want to inform the user of a non-critical error. By default all it does is print an error message in red text on the console. It does not stop a pipeline or a loop from continuing. <code>Throw</code> on the other hand produces what is called a terminating error. If you ... | 379, 526, 24067 | error-handling, powershell, powershell-2.0 | <h1>When should I use Write-Error vs. Throw? Terminating vs. non-terminating errors</h1>
<p>Looking at a Get-WebFile script over on PoshCode, <a href="http://poshcode.org/3226" rel="noreferrer">http://poshcode.org/3226</a>, I noticed this strange-to-me contraption:</p>
<pre><code>$URL_Format_Error = [string]"..."
Writ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 885 | bash | # When should I use Write-Error vs. Throw? Terminating vs. non-terminating errors
Looking at a Get-WebFile script over on PoshCode, <http://poshcode.org/3226>, I noticed this strange-to-me contraption:
```
$URL_Format_Error = [string]"..."
Write-Error $URL_Format_Error
return
```
What is the reason for this as oppos... | `Write-Error` should be used if you want to inform the user of a non-critical error. By default all it does is print an error message in red text on the console. It does not stop a pipeline or a loop from continuing. `Throw` on the other hand produces what is called a terminating error. If you use throw, the pipeline a... |
3679296 | Only get hash value using md5sum (without filename) | 276 | 2010-09-09 18:07:26 | <p>I use <a href="https://linux.die.net/man/1/md5sum" rel="noreferrer">md5sum</a> to generate a hash value for a file.
But I only need to receive the hash value, not the file name.</p>
<pre><code>md5=`md5sum ${my_iso_file}`
echo ${md5}
</code></pre>
<p>Output:</p>
<pre><code>3abb17b66815bc7946cefe727737d295 ./iso/some... | 215,169 | 439,621 | 2024-08-10 10:28:19 | 3,679,803 | 234 | 2010-09-09 19:16:27 | 437,095 | 2021-04-19 15:22:02 | https://stackoverflow.com/q/3679296 | https://stackoverflow.com/a/3679803 | <p>Using <a href="https://en.wikipedia.org/wiki/AWK" rel="noreferrer">AWK</a>:</p>
<pre><code>md5=`md5sum ${my_iso_file} | awk '{ print $1 }'`
</code></pre>
| <p>Using <a href="https://en.wikipedia.org/wiki/AWK" rel="noreferrer">AWK</a>:</p> <pre><code>md5=`md5sum ${my_iso_file} | awk '{ print $1 }'` </code></pre> | 387, 390, 46308 | bash, md5sum, shell | <h1>Only get hash value using md5sum (without filename)</h1>
<p>I use <a href="https://linux.die.net/man/1/md5sum" rel="noreferrer">md5sum</a> to generate a hash value for a file.
But I only need to receive the hash value, not the file name.</p>
<pre><code>md5=`md5sum ${my_iso_file}`
echo ${md5}
</code></pre>
<p>Output... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 886 | bash | # Only get hash value using md5sum (without filename)
I use [md5sum](https://linux.die.net/man/1/md5sum) to generate a hash value for a file.
But I only need to receive the hash value, not the file name.
```
md5=`md5sum ${my_iso_file}`
echo ${md5}
```
Output:
```
3abb17b66815bc7946cefe727737d295 ./iso/somefile.iso... | Using [AWK](https://en.wikipedia.org/wiki/AWK):
```
md5=`md5sum ${my_iso_file} | awk '{ print $1 }'`
``` |
20886243 | Press any key to continue | 151 | 2014-01-02 15:34:15 | <p>According to Microsoft's <a href="http://technet.microsoft.com/en-us/library/ee176935.aspx" rel="noreferrer">documentation</a>, <code>read-host</code> lets the user type some input, and then press enter to continue. Not exactly the correct behavior if you want to have "Press any key to continue". (Wait... where's th... | 341,591 | 945,456 | 2015-04-29 22:33:56 | 20,886,446 | 234 | 2014-01-02 15:44:54 | 3,093,031 | 2014-01-02 17:44:03 | https://stackoverflow.com/q/20886243 | https://stackoverflow.com/a/20886446 | <p>Here is what I use.</p>
<pre><code>Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
</code></pre>
| <p>Here is what I use.</p> <pre><code>Write-Host -NoNewLine 'Press any key to continue...'; $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'); </code></pre> | 526 | powershell | <h1>Press any key to continue</h1>
<p>According to Microsoft's <a href="http://technet.microsoft.com/en-us/library/ee176935.aspx" rel="noreferrer">documentation</a>, <code>read-host</code> lets the user type some input, and then press enter to continue. Not exactly the correct behavior if you want to have "Press any ke... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 887 | bash | # Press any key to continue
According to Microsoft's [documentation](http://technet.microsoft.com/en-us/library/ee176935.aspx), `read-host` lets the user type some input, and then press enter to continue. Not exactly the correct behavior if you want to have "Press any key to continue". (Wait... where's the `Any` key?!... | Here is what I use.
```
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
``` |
9675658 | Powershell Get-ChildItem most recent file in directory | 113 | 2012-03-12 22:23:12 | <p>We produce files with date in the name.
(* below is the wildcard for the date)
I want to grab the last file and the folder that contains the file also has a date(month only) in its title.</p>
<p>I am using PowerShell and I am scheduling it to run each day. Here is the script so far:</p>
<pre><code> $LastFile = *... | 224,098 | 977,645 | 2021-08-15 12:59:38 | 9,675,811 | 234 | 2012-03-12 22:39:56 | 526,535 | 2012-03-12 22:49:35 | https://stackoverflow.com/q/9675658 | https://stackoverflow.com/a/9675811 | <p>If you want the latest file in the directory and you are using only the <code>LastWriteTime</code> to determine the latest file, you can do something like below:</p>
<pre><code>gci path | sort LastWriteTime | select -last 1
</code></pre>
<p>On the other hand, if you want to only rely on the names that have the dat... | <p>If you want the latest file in the directory and you are using only the <code>LastWriteTime</code> to determine the latest file, you can do something like below:</p> <pre><code>gci path | sort LastWriteTime | select -last 1 </code></pre> <p>On the other hand, if you want to only rely on the names that have the dat... | 526 | powershell | <h1>Powershell Get-ChildItem most recent file in directory</h1>
<p>We produce files with date in the name.
(* below is the wildcard for the date)
I want to grab the last file and the folder that contains the file also has a date(month only) in its title.</p>
<p>I am using PowerShell and I am scheduling it to run each ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 888 | bash | # Powershell Get-ChildItem most recent file in directory
We produce files with date in the name.
(* below is the wildcard for the date)
I want to grab the last file and the folder that contains the file also has a date(month only) in its title.
I am using PowerShell and I am scheduling it to run each day. Here is the... | If you want the latest file in the directory and you are using only the `LastWriteTime` to determine the latest file, you can do something like below:
```
gci path | sort LastWriteTime | select -last 1
```
On the other hand, if you want to only rely on the names that have the dates in them, you should be able to some... |
5615717 | How can I store a command in a variable in a shell script? | 201 | 2011-04-11 00:33:13 | <p>I would like to store a command to use at a later time in a variable (not the output of the command, but the command itself).</p>
<p>I have a simple script as follows:</p>
<pre><code>command="ls";
echo "Command: $command"; #Output is: Command: ls
b=`$command`;
echo $b; #Output is: public_html RE... | 340,360 | 678,289 | 2024-01-30 15:39:51 | 5,615,748 | 233 | 2011-04-11 00:38:21 | 616,700 | 2019-02-10 11:07:23 | https://stackoverflow.com/q/5615717 | https://stackoverflow.com/a/5615748 | <p>Use eval:</p>
<pre><code>x="ls | wc"
eval "$x"
y=$(eval "$x")
echo "$y"
</code></pre>
| <p>Use eval:</p> <pre><code>x="ls | wc" eval "$x" y=$(eval "$x") echo "$y" </code></pre> | 58, 276, 387, 1796 | bash, command, linux, variables | <h1>How can I store a command in a variable in a shell script?</h1>
<p>I would like to store a command to use at a later time in a variable (not the output of the command, but the command itself).</p>
<p>I have a simple script as follows:</p>
<pre><code>command="ls";
echo "Command: $command"; #Outpu... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 889 | bash | # How can I store a command in a variable in a shell script?
I would like to store a command to use at a later time in a variable (not the output of the command, but the command itself).
I have a simple script as follows:
```
command="ls";
echo "Command: $command"; #Output is: Command: ls
b=`$command`;
echo $b; #Ou... | Use eval:
```
x="ls | wc"
eval "$x"
y=$(eval "$x")
echo "$y"
``` |
13104800 | Is there a way to 'pretty' print MongoDB shell output to a file? | 113 | 2012-10-27 22:40:50 | <p>Specifically, I want to print the results of a mongodb <code>find()</code> to a file. The JSON object is too large so I'm unable to view the entire object with the shell window size.</p>
| 140,809 | 1,105,341 | 2022-01-17 02:53:09 | 13,111,118 | 233 | 2012-10-28 17:16:13 | 431,012 | 2013-07-12 00:23:54 | https://stackoverflow.com/q/13104800 | https://stackoverflow.com/a/13111118 | <p>The shell provides some nice but hidden features because it's an interactive environment.</p>
<p>When you run commands from a javascript file via mongo commands.js you won't get quite identical behavior.</p>
<p>There are two ways around this.</p>
<p>(1) fake out the shell and make it think you are in interactive ... | <p>The shell provides some nice but hidden features because it's an interactive environment.</p> <p>When you run commands from a javascript file via mongo commands.js you won't get quite identical behavior.</p> <p>There are two ways around this.</p> <p>(1) fake out the shell and make it think you are in interactive ... | 3, 390, 30073 | javascript, mongodb, shell | <h1>Is there a way to 'pretty' print MongoDB shell output to a file?</h1>
<p>Specifically, I want to print the results of a mongodb <code>find()</code> to a file. The JSON object is too large so I'm unable to view the entire object with the shell window size.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 890 | bash | # Is there a way to 'pretty' print MongoDB shell output to a file?
Specifically, I want to print the results of a mongodb `find()` to a file. The JSON object is too large so I'm unable to view the entire object with the shell window size. | The shell provides some nice but hidden features because it's an interactive environment.
When you run commands from a javascript file via mongo commands.js you won't get quite identical behavior.
There are two ways around this.
(1) fake out the shell and make it think you are in interactive mode
```
$ mongo dbname... |
13160309 | Conversion hex string into ascii in bash command line | 91 | 2012-10-31 14:33:38 | <p>I have a lot of this kind of string and I want to find a command to convert it in ascii, I tried with <code>echo -e</code> and <code>od</code>, but it did not work.</p>
<pre><code>0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2
</code></pre>
| 295,155 | 166,034 | 2025-01-09 16:49:58 | 30,084,257 | 233 | 2015-05-06 17:58:56 | 878,969 | 2024-06-26 13:52:41 | https://stackoverflow.com/q/13160309 | https://stackoverflow.com/a/30084257 | <p>This worked for me.</p>
<pre><code>$ echo -n 54657374696e672031203220330 | xxd -r -p
Testing 1 2 3$
</code></pre>
<p><code>echo -n</code> prevents a newline from being added to the end</p>
<p><code>-r</code> tells it to convert hex to ascii as opposed to its normal mode of doing the opposite</p>
<p><code>-p</code> t... | <p>This worked for me.</p> <pre><code>$ echo -n 54657374696e672031203220330 | xxd -r -p Testing 1 2 3$ </code></pre> <p><code>echo -n</code> prevents a newline from being added to the end</p> <p><code>-r</code> tells it to convert hex to ascii as opposed to its normal mode of doing the opposite</p> <p><code>-p</code> t... | 387, 1391 | bash, hex | <h1>Conversion hex string into ascii in bash command line</h1>
<p>I have a lot of this kind of string and I want to find a command to convert it in ascii, I tried with <code>echo -e</code> and <code>od</code>, but it did not work.</p>
<pre><code>0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 891 | bash | # Conversion hex string into ascii in bash command line
I have a lot of this kind of string and I want to find a command to convert it in ascii, I tried with `echo -e` and `od`, but it did not work.
```
0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2
``` | This worked for me.
```
$ echo -n 54657374696e672031203220330 | xxd -r -p
Testing 1 2 3$
```
`echo -n` prevents a newline from being added to the end
`-r` tells it to convert hex to ascii as opposed to its normal mode of doing the opposite
`-p` tells it to use a plain format. |
10137146 | Is there a way to make a PowerShell script work by double clicking a .ps1 file? | 253 | 2012-04-13 07:54:45 | <p>I am distributing a PowerShell script to my team. The script is to fetch an IP address from the Vsphere client, make an mstsc connection, and log it in a shared file.</p>
<p>The moment they used the script they got to know the IP address of machine. After that, they always tend to use mstsc directly instead of runn... | 301,670 | 399,145 | 2024-07-09 19:06:35 | 10,137,272 | 232 | 2012-04-13 08:04:58 | 311,712 | 2012-04-13 08:04:58 | https://stackoverflow.com/q/10137146 | https://stackoverflow.com/a/10137272 | <p>Create a shortcut with something like this as the "Target":</p>
<pre><code>powershell.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah"
</code></pre>
| <p>Create a shortcut with something like this as the "Target":</p> <pre><code>powershell.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah" </code></pre> | 526, 24067 | powershell, powershell-2.0 | <h1>Is there a way to make a PowerShell script work by double clicking a .ps1 file?</h1>
<p>I am distributing a PowerShell script to my team. The script is to fetch an IP address from the Vsphere client, make an mstsc connection, and log it in a shared file.</p>
<p>The moment they used the script they got to know the ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 892 | bash | # Is there a way to make a PowerShell script work by double clicking a .ps1 file?
I am distributing a PowerShell script to my team. The script is to fetch an IP address from the Vsphere client, make an mstsc connection, and log it in a shared file.
The moment they used the script they got to know the IP address of ma... | Create a shortcut with something like this as the "Target":
```
powershell.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah"
``` |
44958847 | Message "the term 'ng' is not recognized as the name of a cmdlet" | 235 | 2017-07-06 21:06:55 | <p>Today, while working through some basic AngularJS introduction, I ran into a problem.</p>
<p>I opened PowerShell to get going on the project. NPM worked.</p>
<p>I was able to install Angular using:</p>
<pre><code>npm install -g @angular/cli
</code></pre>
<p>Anytime I tried to run <em>ng</em>, I would get:</p>
<block... | 816,051 | 8,267,421 | 2025-05-04 18:05:09 | 44,958,882 | 232 | 2017-07-06 21:08:57 | 3,001,761 | 2019-07-16 20:36:00 | https://stackoverflow.com/q/44958847 | https://stackoverflow.com/a/44958882 | <p>The first path in the path variable needs to be the NPM path. Opening the Node.js command prompt I found that the ng command worked there. I dug into the shortcut and found that it references a command to ensure the first Path variable is NPM.
To Fix:</p>
<ol>
<li>Right Clicked on My Computer (windows)</li>
<li>Se... | <p>The first path in the path variable needs to be the NPM path. Opening the Node.js command prompt I found that the ng command worked there. I dug into the shortcut and found that it references a command to ensure the first Path variable is NPM. To Fix:</p> <ol> <li>Right Clicked on My Computer (windows)</li> <li>Se... | 526, 46426, 61387, 117722, 125866 | angular, angular-cli, node.js, npm, powershell | <h1>Message "the term 'ng' is not recognized as the name of a cmdlet"</h1>
<p>Today, while working through some basic AngularJS introduction, I ran into a problem.</p>
<p>I opened PowerShell to get going on the project. NPM worked.</p>
<p>I was able to install Angular using:</p>
<pre><code>npm install -g @angular/cli
<... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 893 | bash | # Message "the term 'ng' is not recognized as the name of a cmdlet"
Today, while working through some basic AngularJS introduction, I ran into a problem.
I opened PowerShell to get going on the project. NPM worked.
I was able to install Angular using:
```
npm install -g @angular/cli
```
Anytime I tried to run *ng*... | The first path in the path variable needs to be the NPM path. Opening the Node.js command prompt I found that the ng command worked there. I dug into the shortcut and found that it references a command to ensure the first Path variable is NPM.
To Fix:
1. Right Clicked on My Computer (windows)
2. Selected Advanced Syst... |
12487424 | uppercase first character in a variable with bash | 272 | 2012-09-19 01:35:37 | <p>I want to uppercase just the first character in my string with bash.</p>
<pre><code>foo="bar";
//uppercase first character
echo $foo;
</code></pre>
<p>should print <code>Bar</code>.</p>
| 192,061 | 33,522 | 2024-07-12 13:45:24 | 12,487,465 | 231 | 2012-09-19 01:41:50 | 494,061 | 2012-09-19 01:41:50 | https://stackoverflow.com/q/12487424 | https://stackoverflow.com/a/12487465 | <pre><code>foo="$(tr '[:lower:]' '[:upper:]' <<< ${foo:0:1})${foo:1}"
</code></pre>
| <pre><code>foo="$(tr '[:lower:]' '[:upper:]' <<< ${foo:0:1})${foo:1}" </code></pre> | 387, 61267 | bash, uppercase | <h1>uppercase first character in a variable with bash</h1>
<p>I want to uppercase just the first character in my string with bash.</p>
<pre><code>foo="bar";
//uppercase first character
echo $foo;
</code></pre>
<p>should print <code>Bar</code>.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 894 | bash | # uppercase first character in a variable with bash
I want to uppercase just the first character in my string with bash.
```
foo="bar";
//uppercase first character
echo $foo;
```
should print `Bar`. | ```
foo="$(tr '[:lower:]' '[:upper:]' <<< ${foo:0:1})${foo:1}"
``` |
2829613 | How do you tell if a string contains another string in POSIX sh? | 191 | 2010-05-13 19:17:33 | <p>I want to write a Unix shell script that will do various logic if there is a string inside of another string. For example, if I am in a certain folder, branch off. Could someone please tell me how to accomplish this? If possible I would like to make this not shell specific (i.e. not bash only) but if there's no othe... | 342,214 | 340,599 | 2024-07-11 02:29:32 | 8,811,800 | 231 | 2012-01-10 23:01:05 | 393,684 | 2023-03-31 06:05:57 | https://stackoverflow.com/q/2829613 | https://stackoverflow.com/a/8811800 | <p>Here's yet another solution. This uses <a href="https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02" rel="noreferrer">POSIX substring parameter expansion</a>, so it works in Bash, Dash, KornShell (ksh), Z shell (zsh), etc. It also supports special characters in strings.</p>
<pre cl... | <p>Here's yet another solution. This uses <a href="https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02" rel="noreferrer">POSIX substring parameter expansion</a>, so it works in Bash, Dash, KornShell (ksh), Z shell (zsh), etc. It also supports special characters in strings.</p> <pre cl... | 34, 390 | shell, unix | <h1>How do you tell if a string contains another string in POSIX sh?</h1>
<p>I want to write a Unix shell script that will do various logic if there is a string inside of another string. For example, if I am in a certain folder, branch off. Could someone please tell me how to accomplish this? If possible I would like t... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 895 | bash | # How do you tell if a string contains another string in POSIX sh?
I want to write a Unix shell script that will do various logic if there is a string inside of another string. For example, if I am in a certain folder, branch off. Could someone please tell me how to accomplish this? If possible I would like to make th... | Here's yet another solution. This uses [POSIX substring parameter expansion](https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02), so it works in Bash, Dash, KornShell (ksh), Z shell (zsh), etc. It also supports special characters in strings.
```
test "${string#*"$word"}" != "$string... |
16908236 | How to execute Python inline from a bash shell | 141 | 2013-06-04 01:03:02 | <p>Is there a Python argument to execute code from the shell without starting up an interactive interpreter or reading from a file?
Something similar to:</p>
<pre><code>perl -e 'print "Hi"'
</code></pre>
| 120,763 | 1,493,578 | 2020-11-18 19:36:54 | 16,908,265 | 230 | 2013-06-04 01:07:14 | 837,534 | 2020-11-18 19:36:54 | https://stackoverflow.com/q/16908236 | https://stackoverflow.com/a/16908265 | <p>This works:</p>
<pre><code>python -c 'print("Hi")'
Hi
</code></pre>
<p>From the manual, <code>man python</code>:</p>
<blockquote>
<pre><code> -c command
Specify the command to execute (see next section). This termi-
nates the option list (following options are passed as arguments
... | <p>This works:</p> <pre><code>python -c 'print("Hi")' Hi </code></pre> <p>From the manual, <code>man python</code>:</p> <blockquote> <pre><code> -c command Specify the command to execute (see next section). This termi- nates the option list (following options are passed as arguments ... | 16, 390, 4847, 11844 | execution, inline, python, shell | <h1>How to execute Python inline from a bash shell</h1>
<p>Is there a Python argument to execute code from the shell without starting up an interactive interpreter or reading from a file?
Something similar to:</p>
<pre><code>perl -e 'print "Hi"'
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 896 | bash | # How to execute Python inline from a bash shell
Is there a Python argument to execute code from the shell without starting up an interactive interpreter or reading from a file?
Something similar to:
```
perl -e 'print "Hi"'
``` | This works:
```
python -c 'print("Hi")'
Hi
```
From the manual, `man python`:
> ```
> -c command
> Specify the command to execute (see next section). This termi-
> nates the option list (following options are passed as arguments
> to the command).
> ``` |
4168371 | How can I remove all text after a character in Bash? | 267 | 2010-11-12 19:34:29 | <p>How can I remove all text after a character, in this case a colon (":"), in Bash?</p>
<p>Can I remove the colon, too? I don’t have any idea how to.</p>
| 383,732 | 317,076 | 2026-01-22 19:23:58 | 4,168,417 | 229 | 2010-11-12 19:41:28 | 386,268 | 2026-01-22 16:43:26 | https://stackoverflow.com/q/4168371 | https://stackoverflow.com/a/4168417 | <p>If I understood you correctly, this would work:</p>
<pre><code>echo "Hello: world" | cut -f1 -d":"
</code></pre>
<p>This will convert <code>Hello: world</code> into <code>Hello</code>.</p>
| <p>If I understood you correctly, this would work:</p> <pre><code>echo "Hello: world" | cut -f1 -d":" </code></pre> <p>This will convert <code>Hello: world</code> into <code>Hello</code>.</p> | 387 | bash | <h1>How can I remove all text after a character in Bash?</h1>
<p>How can I remove all text after a character, in this case a colon (":"), in Bash?</p>
<p>Can I remove the colon, too? I don’t have any idea how to.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 897 | bash | # How can I remove all text after a character in Bash?
How can I remove all text after a character, in this case a colon (":"), in Bash?
Can I remove the colon, too? I don’t have any idea how to. | If I understood you correctly, this would work:
```
echo "Hello: world" | cut -f1 -d":"
```
This will convert `Hello: world` into `Hello`. |
4438147 | Alias with variable in bash | 154 | 2010-12-14 10:29:02 | <p>I want to create an alias in <code>bash</code> like this: </p>
<pre><code>alias tail_ls="ls -l $1 | tail"
</code></pre>
<p>Thus, if somebody types:</p>
<pre><code>tail_ls /etc/
</code></pre>
<p>it will only show the last 10 files in the directory.</p>
<p>But <code>$1</code> does not seem to work for me. Is the... | 142,546 | 317,136 | 2016-06-16 08:58:58 | 4,438,226 | 229 | 2010-12-14 10:36:48 | 141,906 | 2013-03-16 06:07:24 | https://stackoverflow.com/q/4438147 | https://stackoverflow.com/a/4438226 | <p>I'd create a function for that, rather than alias, and then exported it, like this:</p>
<pre><code>function tail_ls { ls -l "$1" | tail; }
export -f tail_ls
</code></pre>
<p>Note <code>-f</code> switch to <code>export</code>: it tells it that you are exporting a function. Put this in your <code>.bashrc</code> and... | <p>I'd create a function for that, rather than alias, and then exported it, like this:</p> <pre><code>function tail_ls { ls -l "$1" | tail; } export -f tail_ls </code></pre> <p>Note <code>-f</code> switch to <code>export</code>: it tells it that you are exporting a function. Put this in your <code>.bashrc</code> and... | 58, 387 | bash, linux | <h1>Alias with variable in bash</h1>
<p>I want to create an alias in <code>bash</code> like this: </p>
<pre><code>alias tail_ls="ls -l $1 | tail"
</code></pre>
<p>Thus, if somebody types:</p>
<pre><code>tail_ls /etc/
</code></pre>
<p>it will only show the last 10 files in the directory.</p>
<p>But <code>$1</code>... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 898 | bash | # Alias with variable in bash
I want to create an alias in `bash` like this:
```
alias tail_ls="ls -l $1 | tail"
```
Thus, if somebody types:
```
tail_ls /etc/
```
it will only show the last 10 files in the directory.
But `$1` does not seem to work for me. Is there any way I can introduce variables in bash. | I'd create a function for that, rather than alias, and then exported it, like this:
```
function tail_ls { ls -l "$1" | tail; }
export -f tail_ls
```
Note `-f` switch to `export`: it tells it that you are exporting a function. Put this in your `.bashrc` and you are good to go. |
18660798 | here-document gives 'unexpected end of file' error | 135 | 2013-09-06 14:59:42 | <p>I need my script to send an email from terminal. Based on what I've seen here and many other places online, I formatted it like this:</p>
<pre class="lang-none prettyprint-override"><code>/var/mail -s "$SUBJECT" "$EMAIL" << EOF
Here's a line of my message!
And here's another line!
Last line of the message her... | 218,483 | 2,661,085 | 2024-11-06 12:33:28 | 18,660,985 | 229 | 2013-09-06 15:10:23 | 1,491,895 | 2019-01-13 07:33:38 | https://stackoverflow.com/q/18660798 | https://stackoverflow.com/a/18660985 | <p>The <code>EOF</code> token must be at the beginning of the line, you can't indent it along with the block of code it goes with.</p>
<p>If you write <code><<-EOF</code> you may indent it, but it must be indented with <kbd>Tab</kbd> characters, not spaces. So it still might not end up even with the block of cod... | <p>The <code>EOF</code> token must be at the beginning of the line, you can't indent it along with the block of code it goes with.</p> <p>If you write <code><<-EOF</code> you may indent it, but it must be indented with <kbd>Tab</kbd> characters, not spaces. So it still might not end up even with the block of cod... | 146, 387, 7449 | bash, email, heredoc | <h1>here-document gives 'unexpected end of file' error</h1>
<p>I need my script to send an email from terminal. Based on what I've seen here and many other places online, I formatted it like this:</p>
<pre class="lang-none prettyprint-override"><code>/var/mail -s "$SUBJECT" "$EMAIL" << EOF
Here's a line of my me... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 899 | bash | # here-document gives 'unexpected end of file' error
I need my script to send an email from terminal. Based on what I've seen here and many other places online, I formatted it like this:
```
/var/mail -s "$SUBJECT" "$EMAIL" << EOF
Here's a line of my message!
And here's another line!
Last line of the message here!
EO... | The `EOF` token must be at the beginning of the line, you can't indent it along with the block of code it goes with.
If you write `<<-EOF` you may indent it, but it must be indented with `Tab` characters, not spaces. So it still might not end up even with the block of code.
Also make sure you have no whitespace **aft... |
2297510 | Linux Shell Script For Each File in a Directory Grab the filename and execute a program | 106 | 2010-02-19 15:43:34 | <p>Scenario :</p>
<p>A folder in Linux system. I want to loop through every .xls file in a folder.</p>
<p>This folder typically consists of various folders, various filetypes (.sh, .pl,.csv,...).</p>
<p>All I want to do is loop through <strong>all files in the root</strong> and execute a program only on .xls files.<... | 186,441 | 235,310 | 2015-01-27 23:23:37 | 2,297,719 | 229 | 2010-02-19 16:10:02 | 20,862 | 2010-02-19 16:10:02 | https://stackoverflow.com/q/2297510 | https://stackoverflow.com/a/2297719 | <p>bash:</p>
<pre><code>for f in *.xls ; do xls2csv "$f" "${f%.xls}.csv" ; done
</code></pre>
| <p>bash:</p> <pre><code>for f in *.xls ; do xls2csv "$f" "${f%.xls}.csv" ; done </code></pre> | 58, 390 | linux, shell | <h1>Linux Shell Script For Each File in a Directory Grab the filename and execute a program</h1>
<p>Scenario :</p>
<p>A folder in Linux system. I want to loop through every .xls file in a folder.</p>
<p>This folder typically consists of various folders, various filetypes (.sh, .pl,.csv,...).</p>
<p>All I want to do ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 900 | bash | # Linux Shell Script For Each File in a Directory Grab the filename and execute a program
Scenario :
A folder in Linux system. I want to loop through every .xls file in a folder.
This folder typically consists of various folders, various filetypes (.sh, .pl,.csv,...).
All I want to do is loop through **all files in... | bash:
```
for f in *.xls ; do xls2csv "$f" "${f%.xls}.csv" ; done
``` |
3494115 | What does $_ mean in PowerShell? | 324 | 2010-08-16 14:33:33 | <p>I've seen the following a lot in PowerShell, but what does it do exactly?</p>
<pre><code>$_
</code></pre>
| 405,531 | 17,744 | 2022-12-21 18:28:10 | 3,494,138 | 228 | 2010-08-16 14:36:08 | 23,283 | 2018-09-03 12:12:06 | https://stackoverflow.com/q/3494115 | https://stackoverflow.com/a/3494138 | <p>This is the variable for the current value in the pipe line, which is called <code>$PSItem</code> in Powershell 3 and newer. </p>
<pre><code>1,2,3 | %{ write-host $_ }
</code></pre>
<p>or</p>
<pre><code>1,2,3 | %{ write-host $PSItem }
</code></pre>
<p>For example in the above code the <code>%{}</code> block i... | <p>This is the variable for the current value in the pipe line, which is called <code>$PSItem</code> in Powershell 3 and newer. </p> <pre><code>1,2,3 | %{ write-host $_ } </code></pre> <p>or</p> <pre><code>1,2,3 | %{ write-host $PSItem } </code></pre> <p>For example in the above code the <code>%{}</code> block i... | 526 | powershell | <h1>What does $_ mean in PowerShell?</h1>
<p>I've seen the following a lot in PowerShell, but what does it do exactly?</p>
<pre><code>$_
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 901 | bash | # What does $_ mean in PowerShell?
I've seen the following a lot in PowerShell, but what does it do exactly?
```
$_
``` | This is the variable for the current value in the pipe line, which is called `$PSItem` in Powershell 3 and newer.
```
1,2,3 | %{ write-host $_ }
```
or
```
1,2,3 | %{ write-host $PSItem }
```
For example in the above code the `%{}` block is called for every value in the array. The `$_` or `$PSItem` variable will co... |
34937724 | Running bash scripts with npm | 149 | 2016-01-22 01:54:33 | <p>I want to try using npm to run my various build tasks for a web application. I know I can do this by adding a <code>scripts</code> field to my <code>package.json</code> like so:</p>
<pre><code>"scripts": {
"build": "some build command"
},
</code></pre>
<p>This gets unwieldy when you have more complex commands ... | 200,485 | 2,962,432 | 2025-08-23 02:50:01 | 34,938,559 | 228 | 2016-01-22 03:35:15 | 1,416,519 | 2018-09-21 20:14:59 | https://stackoverflow.com/q/34937724 | https://stackoverflow.com/a/34938559 | <p>Its totally possible... </p>
<pre><code>"scripts": {
"build": "./build.sh"
},
</code></pre>
<p>also, make sure you put a hash bang at the top of your bash file <code>#!/usr/bin/env bash</code></p>
<p>also make sure you have permissions to execute the file </p>
<pre><code>chmod +x ./build.sh
</code></pre>
<p>... | <p>Its totally possible... </p> <pre><code>"scripts": { "build": "./build.sh" }, </code></pre> <p>also, make sure you put a hash bang at the top of your bash file <code>#!/usr/bin/env bash</code></p> <p>also make sure you have permissions to execute the file </p> <pre><code>chmod +x ./build.sh </code></pre> <p>... | 387, 390, 46426, 61387 | bash, node.js, npm, shell | <h1>Running bash scripts with npm</h1>
<p>I want to try using npm to run my various build tasks for a web application. I know I can do this by adding a <code>scripts</code> field to my <code>package.json</code> like so:</p>
<pre><code>"scripts": {
"build": "some build command"
},
</code></pre>
<p>This gets unwiel... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 902 | bash | # Running bash scripts with npm
I want to try using npm to run my various build tasks for a web application. I know I can do this by adding a `scripts` field to my `package.json` like so:
```
"scripts": {
"build": "some build command"
},
```
This gets unwieldy when you have more complex commands with a bunch of o... | Its totally possible...
```
"scripts": {
"build": "./build.sh"
},
```
also, make sure you put a hash bang at the top of your bash file `#!/usr/bin/env bash`
also make sure you have permissions to execute the file
```
chmod +x ./build.sh
```
Finally, the command to run build in npm would be
```
npm run build
``... |
6871859 | Piping command output to tee but also save exit code of command | 275 | 2011-07-29 10:36:42 | <p>I have a shell script in which I wrap a command (mvn clean install), to redirect the output to a logfile. </p>
<pre><code>#!/bin/bash
...
mvn clean install $@ | tee $logfile
echo $? # Does not show the return code of mvn clean install
</code></pre>
<p>Now if <code>mvn clean install</code> fails with an error, I w... | 102,569 | 319,905 | 2016-09-27 16:57:05 | 6,871,917 | 227 | 2011-07-29 10:42:19 | 464,709 | 2011-07-29 10:42:19 | https://stackoverflow.com/q/6871859 | https://stackoverflow.com/a/6871917 | <p>Since you're running <code>bash</code>, you can use its <a href="http://tldp.org/LDP/abs/html/internalvariables.html#PIPESTATUSREF" rel="noreferrer">$PIPESTATUS</a> variable instead of <code>$?</code>:</p>
<pre><code>mvn clean install $@ | tee $logfile
echo ${PIPESTATUS[0]}
</code></pre>
| <p>Since you're running <code>bash</code>, you can use its <a href="http://tldp.org/LDP/abs/html/internalvariables.html#PIPESTATUSREF" rel="noreferrer">$PIPESTATUS</a> variable instead of <code>$?</code>:</p> <pre><code>mvn clean install $@ | tee $logfile echo ${PIPESTATUS[0]} </code></pre> | 387, 390, 5813, 10327, 22055 | bash, pipe, sh, shell, tee | <h1>Piping command output to tee but also save exit code of command</h1>
<p>I have a shell script in which I wrap a command (mvn clean install), to redirect the output to a logfile. </p>
<pre><code>#!/bin/bash
...
mvn clean install $@ | tee $logfile
echo $? # Does not show the return code of mvn clean install
</code>... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 903 | bash | # Piping command output to tee but also save exit code of command
I have a shell script in which I wrap a command (mvn clean install), to redirect the output to a logfile.
```
#!/bin/bash
...
mvn clean install $@ | tee $logfile
echo $? # Does not show the return code of mvn clean install
```
Now if `mvn clean instal... | Since you're running `bash`, you can use its [$PIPESTATUS](http://tldp.org/LDP/abs/html/internalvariables.html#PIPESTATUSREF) variable instead of `$?`:
```
mvn clean install $@ | tee $logfile
echo ${PIPESTATUS[0]}
``` |
2621513 | How to use find command to find all files with extensions from list? | 215 | 2010-04-12 11:07:20 | <p>I need to find all image files from directory (gif, png, jpg, jpeg).</p>
<pre><code>find /path/to/ -name "*.jpg" > log
</code></pre>
<p>How to modify this string to find not only .jpg files?</p>
| 306,945 | 227,523 | 2025-11-04 16:54:54 | 2,622,857 | 227 | 2010-04-12 14:38:19 | 26,428 | 2010-04-12 14:38:19 | https://stackoverflow.com/q/2621513 | https://stackoverflow.com/a/2622857 | <pre><code>find /path/to -regex ".*\.\(jpg\|gif\|png\|jpeg\)" > log
</code></pre>
| <pre><code>find /path/to -regex ".*\.\(jpg\|gif\|png\|jpeg\)" > log </code></pre> | 34, 390, 10193 | find, shell, unix | <h1>How to use find command to find all files with extensions from list?</h1>
<p>I need to find all image files from directory (gif, png, jpg, jpeg).</p>
<pre><code>find /path/to/ -name "*.jpg" > log
</code></pre>
<p>How to modify this string to find not only .jpg files?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 904 | bash | # How to use find command to find all files with extensions from list?
I need to find all image files from directory (gif, png, jpg, jpeg).
```
find /path/to/ -name "*.jpg" > log
```
How to modify this string to find not only .jpg files? | ```
find /path/to -regex ".*\.\(jpg\|gif\|png\|jpeg\)" > log
``` |
2480584 | How do I use a file grep comparison inside a bash if/else statement? | 155 | 2010-03-19 21:06:22 | <p>When our server comes up we need to check a file to see how the server is configured. </p>
<p>We want to search for the following string inside our /etc/aws/hosts.conf file: </p>
<pre><code>MYSQL_ROLE=master
</code></pre>
<p>Then, we want to test whether that string exists and use an if/else statement to run one... | 248,922 | 77,413 | 2021-09-21 14:44:06 | 2,480,611 | 227 | 2010-03-19 21:10:40 | null | 2010-03-19 21:17:35 | https://stackoverflow.com/q/2480584 | https://stackoverflow.com/a/2480611 | <p>From <code>grep --help</code>, but also see <a href="http://google.com/search?q=man+grep" rel="noreferrer">man grep</a>:</p>
<blockquote>
<p>Exit status is 0 if any line was selected, 1 otherwise;
if any error occurs and -q was not given, the exit status is 2.</p>
</blockquote>
<pre><code>if grep --quiet MYSQL... | <p>From <code>grep --help</code>, but also see <a href="http://google.com/search?q=man+grep" rel="noreferrer">man grep</a>:</p> <blockquote> <p>Exit status is 0 if any line was selected, 1 otherwise; if any error occurs and -q was not given, the exit status is 2.</p> </blockquote> <pre><code>if grep --quiet MYSQL... | 387 | bash | <h1>How do I use a file grep comparison inside a bash if/else statement?</h1>
<p>When our server comes up we need to check a file to see how the server is configured. </p>
<p>We want to search for the following string inside our /etc/aws/hosts.conf file: </p>
<pre><code>MYSQL_ROLE=master
</code></pre>
<p>Then, we w... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 905 | bash | # How do I use a file grep comparison inside a bash if/else statement?
When our server comes up we need to check a file to see how the server is configured.
We want to search for the following string inside our /etc/aws/hosts.conf file:
```
MYSQL_ROLE=master
```
Then, we want to test whether that string exists and ... | From `grep --help`, but also see [man grep](http://google.com/search?q=man+grep):
> Exit status is 0 if any line was selected, 1 otherwise;
> if any error occurs and -q was not given, the exit status is 2.
```
if grep --quiet MYSQL_ROLE=master /etc/aws/hosts.conf; then
echo exists
else
echo not found
fi
```
You ... |
1507816 | With bash, how can I pipe standard error into another process? | 182 | 2009-10-02 05:11:30 | <p>It's well known how to pipe the standard ouput of a process into another processes standard input:</p>
<pre><code>proc1 | proc2
</code></pre>
<p>But what if I want to send the standard error of proc1 to proc2 and leave the standard output going to its current location? You would think <code>bash</code> would have ... | 96,242 | 14,860 | 2023-12-16 02:58:26 | 9,888,720 | 226 | 2012-03-27 11:28:26 | 1,295,381 | 2014-07-01 19:06:20 | https://stackoverflow.com/q/1507816 | https://stackoverflow.com/a/9888720 | <p>There is also <a href="https://en.wikipedia.org/wiki/Process_substitution" rel="noreferrer">process substitution</a>. Which makes a process substitute for a file.<br>
You can send <code>stderr</code> to a file as follows:</p>
<pre><code>process1 2> file
</code></pre>
<p>But you can substitute a process for the... | <p>There is also <a href="https://en.wikipedia.org/wiki/Process_substitution" rel="noreferrer">process substitution</a>. Which makes a process substitute for a file.<br> You can send <code>stderr</code> to a file as follows:</p> <pre><code>process1 2> file </code></pre> <p>But you can substitute a process for the... | 387, 5813, 19156 | bash, pipe, stderr | <h1>With bash, how can I pipe standard error into another process?</h1>
<p>It's well known how to pipe the standard ouput of a process into another processes standard input:</p>
<pre><code>proc1 | proc2
</code></pre>
<p>But what if I want to send the standard error of proc1 to proc2 and leave the standard output goin... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 906 | bash | # With bash, how can I pipe standard error into another process?
It's well known how to pipe the standard ouput of a process into another processes standard input:
```
proc1 | proc2
```
But what if I want to send the standard error of proc1 to proc2 and leave the standard output going to its current location? You wo... | There is also [process substitution](https://en.wikipedia.org/wiki/Process_substitution). Which makes a process substitute for a file.
You can send `stderr` to a file as follows:
```
process1 2> file
```
But you can substitute a process for the file as follows:
```
process1 2> >(process2)
```
Here is a concrete e... |
7110119 | Bash history without line numbers | 143 | 2011-08-18 15:37:38 | <p>The bash <code>history</code> command is very cool. I understand why it shows the line numbers, but is there a way I can invoke the history command and suppress the line numbers?</p>
<p>The point here is to use the history command, so please don't reply <code>cat ~/.bash_history</code></p>
<p>Current Output:</p>
... | 92,314 | 288,032 | 2021-10-27 22:30:25 | 7,110,197 | 226 | 2011-08-18 15:42:21 | 253,056 | 2011-08-18 15:42:21 | https://stackoverflow.com/q/7110119 | https://stackoverflow.com/a/7110197 | <p>Try this:</p>
<pre><code>$ history | cut -c 8-
</code></pre>
| <p>Try this:</p> <pre><code>$ history | cut -c 8- </code></pre> | 58, 387 | bash, linux | <h1>Bash history without line numbers</h1>
<p>The bash <code>history</code> command is very cool. I understand why it shows the line numbers, but is there a way I can invoke the history command and suppress the line numbers?</p>
<p>The point here is to use the history command, so please don't reply <code>cat ~/.bash_h... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 907 | bash | # Bash history without line numbers
The bash `history` command is very cool. I understand why it shows the line numbers, but is there a way I can invoke the history command and suppress the line numbers?
The point here is to use the history command, so please don't reply `cat ~/.bash_history`
Current Output:
```
... | Try this:
```
$ history | cut -c 8-
``` |
43267413 | How to set environment variables from .env file | 122 | 2017-04-06 23:36:24 | <p>Let's say I have .env file contains lines like below:</p>
<pre><code>USERNAME=ABC
PASSWORD=PASS
</code></pre>
<p>Unlike the normal ones have <code>export</code> prefix so I cannot source the file directly.</p>
<p>What's the easiest way to create a shell script that loads content from .env file and set them as env... | 190,525 | 342,553 | 2025-07-28 13:37:53 | 43,267,603 | 226 | 2017-04-06 23:57:20 | 14,122 | 2022-07-12 21:39:03 | https://stackoverflow.com/q/43267413 | https://stackoverflow.com/a/43267603 | <h3>If your lines are valid, trusted shell <em>but for</em> the <code>export</code> command</h3>
<p>This requires appropriate shell quoting. It's thus appropriate if you would have a line like <code>foo='bar baz'</code>, but not if that same line would be written <code>foo=bar baz</code></p>
<pre><code>set -a # automat... | <h3>If your lines are valid, trusted shell <em>but for</em> the <code>export</code> command</h3> <p>This requires appropriate shell quoting. It's thus appropriate if you would have a line like <code>foo='bar baz'</code>, but not if that same line would be written <code>foo=bar baz</code></p> <pre><code>set -a # automat... | 390 | shell | <h1>How to set environment variables from .env file</h1>
<p>Let's say I have .env file contains lines like below:</p>
<pre><code>USERNAME=ABC
PASSWORD=PASS
</code></pre>
<p>Unlike the normal ones have <code>export</code> prefix so I cannot source the file directly.</p>
<p>What's the easiest way to create a shell scr... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 908 | bash | # How to set environment variables from .env file
Let's say I have .env file contains lines like below:
```
USERNAME=ABC
PASSWORD=PASS
```
Unlike the normal ones have `export` prefix so I cannot source the file directly.
What's the easiest way to create a shell script that loads content from .env file and set them ... | ### If your lines are valid, trusted shell *but for* the `export` command
This requires appropriate shell quoting. It's thus appropriate if you would have a line like `foo='bar baz'`, but not if that same line would be written `foo=bar baz`
```
set -a # automatically export all variables
source .env
set +a
```
---
... |
17830326 | Ignoring specific errors in a shell script | 116 | 2013-07-24 09:38:53 | <p>I have a small snippet of a shell script which has the potential to throw many errors. I have the script currently set to globally stop on all errors. However i would like for this small sub-section is slightly different.</p>
<p>Here is the snippet:</p>
<pre><code>recover database using backup controlfile until ca... | 171,778 | 1,070,950 | 2021-08-06 15:45:28 | 17,830,521 | 226 | 2013-07-24 09:49:06 | 2,235,132 | 2021-08-06 15:45:28 | https://stackoverflow.com/q/17830326 | https://stackoverflow.com/a/17830521 | <p>In order to cause bash to ignore errors for specific commands you can say:</p>
<pre><code>some-arbitrary-command || true
</code></pre>
<p>This would make the script continue. For example, if you have the following script:</p>
<pre><code>$ cat foo
set -e
echo 1
some-arbitrary-command || true
echo 2
</code></pre>
<p>... | <p>In order to cause bash to ignore errors for specific commands you can say:</p> <pre><code>some-arbitrary-command || true </code></pre> <p>This would make the script continue. For example, if you have the following script:</p> <pre><code>$ cat foo set -e echo 1 some-arbitrary-command || true echo 2 </code></pre> <p>... | 379, 387, 390 | bash, error-handling, shell | <h1>Ignoring specific errors in a shell script</h1>
<p>I have a small snippet of a shell script which has the potential to throw many errors. I have the script currently set to globally stop on all errors. However i would like for this small sub-section is slightly different.</p>
<p>Here is the snippet:</p>
<pre><cod... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 909 | bash | # Ignoring specific errors in a shell script
I have a small snippet of a shell script which has the potential to throw many errors. I have the script currently set to globally stop on all errors. However i would like for this small sub-section is slightly different.
Here is the snippet:
```
recover database using ba... | In order to cause bash to ignore errors for specific commands you can say:
```
some-arbitrary-command || true
```
This would make the script continue. For example, if you have the following script:
```
$ cat foo
set -e
echo 1
some-arbitrary-command || true
echo 2
```
Executing it would return:
```
$ bash foo
1
z: ... |
20361982 | Get current time in hours and minutes | 112 | 2013-12-03 21:25:00 | <p>I'm trying to collect information from a system and I need to get the current time in hours and minutes.</p>
<p>Currently I have:</p>
<pre><code>date | awk '{print $4}'
</code></pre>
<p>which outputs something like:</p>
<pre><code>16:18:54
</code></pre>
<p>How do I chop off the seconds?</p>
| 241,777 | 2,808,815 | 2021-01-28 22:29:26 | 20,362,039 | 226 | 2013-12-03 21:28:13 | 45,112 | 2016-05-13 16:52:39 | https://stackoverflow.com/q/20361982 | https://stackoverflow.com/a/20362039 | <p>Provide a format string:</p>
<pre><code>date +"%H:%M"
</code></pre>
<p>Running <code>man date</code> will give all the format options</p>
<pre class="lang-none prettyprint-override"><code>%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbrevia... | <p>Provide a format string:</p> <pre><code>date +"%H:%M" </code></pre> <p>Running <code>man date</code> will give all the format options</p> <pre class="lang-none prettyprint-override"><code>%a locale's abbreviated weekday name (e.g., Sun) %A locale's full weekday name (e.g., Sunday) %b locale's abbrevia... | 58, 387, 603 | bash, linux, time | <h1>Get current time in hours and minutes</h1>
<p>I'm trying to collect information from a system and I need to get the current time in hours and minutes.</p>
<p>Currently I have:</p>
<pre><code>date | awk '{print $4}'
</code></pre>
<p>which outputs something like:</p>
<pre><code>16:18:54
</code></pre>
<p>How do I... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 910 | bash | # Get current time in hours and minutes
I'm trying to collect information from a system and I need to get the current time in hours and minutes.
Currently I have:
```
date | awk '{print $4}'
```
which outputs something like:
```
16:18:54
```
How do I chop off the seconds? | Provide a format string:
```
date +"%H:%M"
```
Running `man date` will give all the format options
```
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale... |
9994295 | What does $@ mean in a shell script? | 399 | 2012-04-03 13:28:30 | <p>What does a dollar sign followed by an at-sign (<code>@</code>) mean in a shell script?</p>
<p>For example:</p>
<pre><code>umbrella_corp_options $@
</code></pre>
| 390,890 | 454,780 | 2025-05-04 13:33:14 | 9,995,322 | 225 | 2012-04-03 14:26:29 | 1,281,485 | 2019-06-11 09:09:41 | https://stackoverflow.com/q/9994295 | https://stackoverflow.com/a/9995322 | <p><code>$@</code> is nearly the same as <code>$*</code>, both meaning "all command line arguments". They are often used to simply pass all arguments to another program (thus forming a wrapper around that other program).</p>
<p>The difference between the two syntaxes shows up when you have an argument with spaces in ... | <p><code>$@</code> is nearly the same as <code>$*</code>, both meaning "all command line arguments". They are often used to simply pass all arguments to another program (thus forming a wrapper around that other program).</p> <p>The difference between the two syntaxes shows up when you have an argument with spaces in ... | 34, 58, 387, 390, 10327 | bash, linux, sh, shell, unix | <h1>What does $@ mean in a shell script?</h1>
<p>What does a dollar sign followed by an at-sign (<code>@</code>) mean in a shell script?</p>
<p>For example:</p>
<pre><code>umbrella_corp_options $@
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 911 | bash | # What does $@ mean in a shell script?
What does a dollar sign followed by an at-sign (`@`) mean in a shell script?
For example:
```
umbrella_corp_options $@
``` | `$@` is nearly the same as `$*`, both meaning "all command line arguments". They are often used to simply pass all arguments to another program (thus forming a wrapper around that other program).
The difference between the two syntaxes shows up when you have an argument with spaces in it (e.g.) and put `$@` in double ... |
1983048 | Passing a string with spaces as a function argument in Bash | 244 | 2009-12-30 23:23:21 | <p>I'm writing a Bash script where I need to pass a string containing spaces to a function in my Bash script.</p>
<p>For example:</p>
<pre><code>#!/bin/bash
myFunction
{
echo $1
echo $2
echo $3
}
myFunction "firstString" "second string with spaces" "thirdString"
</code></pre>... | 369,473 | 27,314 | 2021-09-16 18:32:10 | 1,983,070 | 225 | 2009-12-30 23:28:11 | 131,527 | 2021-09-16 18:24:13 | https://stackoverflow.com/q/1983048 | https://stackoverflow.com/a/1983070 | <p>You should add quotes and also, your function declaration is wrong.</p>
<pre><code>myFunction()
{
echo "$1"
echo "$2"
echo "$3"
}
</code></pre>
<p>And like the others, it works for me as well.</p>
| <p>You should add quotes and also, your function declaration is wrong.</p> <pre><code>myFunction() { echo "$1" echo "$2" echo "$3" } </code></pre> <p>And like the others, it works for me as well.</p> | 387, 5569 | bash, function | <h1>Passing a string with spaces as a function argument in Bash</h1>
<p>I'm writing a Bash script where I need to pass a string containing spaces to a function in my Bash script.</p>
<p>For example:</p>
<pre><code>#!/bin/bash
myFunction
{
echo $1
echo $2
echo $3
}
myFunction "firstString" "... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 912 | bash | # Passing a string with spaces as a function argument in Bash
I'm writing a Bash script where I need to pass a string containing spaces to a function in my Bash script.
For example:
```
#!/bin/bash
myFunction
{
echo $1
echo $2
echo $3
}
myFunction "firstString" "second string with spaces" "thirdString"... | You should add quotes and also, your function declaration is wrong.
```
myFunction()
{
echo "$1"
echo "$2"
echo "$3"
}
```
And like the others, it works for me as well. |
8762274 | Can I grep only the first n lines of a file? | 162 | 2012-01-06 18:01:15 | <p>I have very long log files, is it possible to ask grep to only search the first 10 lines?</p>
| 269,790 | 199,217 | 2024-12-26 06:18:19 | 8,762,319 | 225 | 2012-01-06 18:03:53 | 477,878 | 2012-01-06 18:03:53 | https://stackoverflow.com/q/8762274 | https://stackoverflow.com/a/8762319 | <p>The magic of pipes;</p>
<pre><code>head -10 log.txt | grep <whatever>
</code></pre>
| <p>The magic of pipes;</p> <pre><code>head -10 log.txt | grep <whatever> </code></pre> | 387, 816, 1271 | bash, grep, search | <h1>Can I grep only the first n lines of a file?</h1>
<p>I have very long log files, is it possible to ask grep to only search the first 10 lines?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 913 | bash | # Can I grep only the first n lines of a file?
I have very long log files, is it possible to ask grep to only search the first 10 lines? | The magic of pipes;
```
head -10 log.txt | grep <whatever>
``` |
14617041 | How can I see the current value of my $PATH variable on OS X? | 144 | 2013-01-31 01:34:00 | <blockquote>
<p>$ $PATH</p>
</blockquote>
<p>returns:</p>
<blockquote>
<p>-bash: /usr/local/share/npm/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/local/sbin:~/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/... | 367,334 | 1,199,195 | 2023-10-20 22:18:25 | 14,617,143 | 225 | 2013-01-31 01:47:43 | 1,411,277 | 2013-01-31 01:47:43 | https://stackoverflow.com/q/14617041 | https://stackoverflow.com/a/14617143 | <p>You need to use the command <code>echo $PATH</code> to display the PATH variable or you can just execute <code>set</code> or <code>env</code> to display all of your environment variables.</p>
<p>By typing <code>$PATH</code> you tried to run your PATH variable contents as a command name.</p>
<p>Bash displayed the c... | <p>You need to use the command <code>echo $PATH</code> to display the PATH variable or you can just execute <code>set</code> or <code>env</code> to display all of your environment variables.</p> <p>By typing <code>$PATH</code> you tried to run your PATH variable contents as a command name.</p> <p>Bash displayed the c... | 369, 387, 1067, 6268, 9013 | bash, environment-variables, homebrew, macos, path | <h1>How can I see the current value of my $PATH variable on OS X?</h1>
<blockquote>
<p>$ $PATH</p>
</blockquote>
<p>returns:</p>
<blockquote>
<p>-bash: /usr/local/share/npm/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/local/sbin:~/bin:/Library/Frameworks/Python.framework/Versions/... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 914 | bash | # How can I see the current value of my $PATH variable on OS X?
> $ $PATH
returns:
> -bash: /usr/local/share/npm/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/local/sbin:~/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X... | You need to use the command `echo $PATH` to display the PATH variable or you can just execute `set` or `env` to display all of your environment variables.
By typing `$PATH` you tried to run your PATH variable contents as a command name.
Bash displayed the contents of your path any way. Based on your output the follow... |
13341832 | Display two files side by side | 130 | 2012-11-12 10:15:29 | <p>How can 2 unsorted text files of different lengths be display side by side <em>(in columns)</em> in a <code>shell</code> </p>
<p>Given <code>one.txt</code> and <code>two.txt</code>:</p>
<pre><code>$ cat one.txt
apple
pear
longer line than the last two
last line
$ cat two.txt
The quick brown fox..
foo
bar
linux
... | 94,904 | 1,066,031 | 2024-06-27 22:38:04 | 13,343,943 | 225 | 2012-11-12 12:44:51 | 20,270 | 2024-06-27 22:38:04 | https://stackoverflow.com/q/13341832 | https://stackoverflow.com/a/13343943 | <p>You can use <a href="https://man7.org/linux/man-pages/man1/pr.1.html" rel="noreferrer"><code>pr</code></a> to do this, using the <code>-m</code> flag to merge the files, one per column, and <code>-t</code> to omit headers, eg.</p>
<pre><code>pr -m -t one.txt two.txt
</code></pre>
<p>outputs:</p>
<pre><code>apple ... | <p>You can use <a href="https://man7.org/linux/man-pages/man1/pr.1.html" rel="noreferrer"><code>pr</code></a> to do this, using the <code>-m</code> flag to merge the files, one per column, and <code>-t</code> to omit headers, eg.</p> <pre><code>pr -m -t one.txt two.txt </code></pre> <p>outputs:</p> <pre><code>apple ... | 34, 58, 390, 1231, 24516 | command-line, gnu-coreutils, linux, shell, unix | <h1>Display two files side by side</h1>
<p>How can 2 unsorted text files of different lengths be display side by side <em>(in columns)</em> in a <code>shell</code> </p>
<p>Given <code>one.txt</code> and <code>two.txt</code>:</p>
<pre><code>$ cat one.txt
apple
pear
longer line than the last two
last line
$ cat two.tx... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 915 | bash | # Display two files side by side
How can 2 unsorted text files of different lengths be display side by side *(in columns)* in a `shell`
Given `one.txt` and `two.txt`:
```
$ cat one.txt
apple
pear
longer line than the last two
last line
$ cat two.txt
The quick brown fox..
foo
bar
linux
skipped a line
```
Display:... | You can use [`pr`](https://man7.org/linux/man-pages/man1/pr.1.html) to do this, using the `-m` flag to merge the files, one per column, and `-t` to omit headers, eg.
```
pr -m -t one.txt two.txt
```
outputs:
```
apple The quick brown fox..
pear foo
longer ... |
6136022 | Script to get the HTTP status code of a list of urls? | 112 | 2011-05-26 08:58:37 | <p>I have a list of URLS that I need to check, to see if they still work or not. I would like to write a bash script that does that for me.</p>
<p>I only need the returned HTTP status code, i.e. 200, 404, 500 and so forth. Nothing more.</p>
<p><em>EDIT</em> Note that there is an issue if the page says "404 not found... | 178,203 | 113,305 | 2023-05-30 14:15:48 | 6,136,861 | 225 | 2011-05-26 10:07:21 | 432,775 | 2018-03-25 23:44:44 | https://stackoverflow.com/q/6136022 | https://stackoverflow.com/a/6136861 | <p>Curl has a specific option, <code>--write-out</code>, for this:</p>
<pre><code>$ curl -o /dev/null --silent --head --write-out '%{http_code}\n' <url>
200
</code></pre>
<ul>
<li><code>-o /dev/null</code> throws away the usual output</li>
<li><code>--silent</code> throws away the progress meter</li>
<li><code>... | <p>Curl has a specific option, <code>--write-out</code>, for this:</p> <pre><code>$ curl -o /dev/null --silent --head --write-out '%{http_code}\n' <url> 200 </code></pre> <ul> <li><code>-o /dev/null</code> throws away the usual output</li> <li><code>--silent</code> throws away the progress meter</li> <li><code>... | 387, 1554, 12832 | bash, curl, http-status-codes | <h1>Script to get the HTTP status code of a list of urls?</h1>
<p>I have a list of URLS that I need to check, to see if they still work or not. I would like to write a bash script that does that for me.</p>
<p>I only need the returned HTTP status code, i.e. 200, 404, 500 and so forth. Nothing more.</p>
<p><em>EDIT</... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 916 | bash | # Script to get the HTTP status code of a list of urls?
I have a list of URLS that I need to check, to see if they still work or not. I would like to write a bash script that does that for me.
I only need the returned HTTP status code, i.e. 200, 404, 500 and so forth. Nothing more.
*EDIT* Note that there is an issue... | Curl has a specific option, `--write-out`, for this:
```
$ curl -o /dev/null --silent --head --write-out '%{http_code}\n' <url>
200
```
- `-o /dev/null` throws away the usual output
- `--silent` throws away the progress meter
- `--head` makes a HEAD HTTP request, instead of GET
- `--write-out '%{http_code}\n'` prints... |
45776003 | Fixing a systemd service 203/EXEC failure (no such file or directory) | 99 | 2017-08-19 20:28:08 | <p>I'm trying to set up a simple systemd timer to run a bash script every day at midnight.</p>
<p><code>systemctl --user status backup.service</code> fails and logs the following:</p>
<pre><code>backup.service: Failed at step EXEC spawning /home/user/.scripts/backup.sh: No such file or directory.
backup.service: Mai... | 355,499 | 6,775,344 | 2022-03-04 11:35:05 | 45,778,018 | 225 | 2017-08-20 02:51:28 | 6,775,344 | 2017-08-22 00:55:57 | https://stackoverflow.com/q/45776003 | https://stackoverflow.com/a/45778018 | <p>I think I found the answer:</p>
<p>In the <code>.service</code> file, I needed to add <code>/bin/bash</code> before the path to the script.</p>
<p>For example, for backup.service:</p>
<p><code>ExecStart=/bin/bash /home/user/.scripts/backup.sh</code></p>
<p>As opposed to:</p>
<p><code>ExecStart=/home/user/.scrip... | <p>I think I found the answer:</p> <p>In the <code>.service</code> file, I needed to add <code>/bin/bash</code> before the path to the script.</p> <p>For example, for backup.service:</p> <p><code>ExecStart=/bin/bash /home/user/.scripts/backup.sh</code></p> <p>As opposed to:</p> <p><code>ExecStart=/home/user/.scrip... | 387, 81442 | bash, systemd | <h1>Fixing a systemd service 203/EXEC failure (no such file or directory)</h1>
<p>I'm trying to set up a simple systemd timer to run a bash script every day at midnight.</p>
<p><code>systemctl --user status backup.service</code> fails and logs the following:</p>
<pre><code>backup.service: Failed at step EXEC spawning... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 917 | bash | # Fixing a systemd service 203/EXEC failure (no such file or directory)
I'm trying to set up a simple systemd timer to run a bash script every day at midnight.
`systemctl --user status backup.service` fails and logs the following:
```
backup.service: Failed at step EXEC spawning /home/user/.scripts/backup.sh: No suc... | I think I found the answer:
In the `.service` file, I needed to add `/bin/bash` before the path to the script.
For example, for backup.service:
`ExecStart=/bin/bash /home/user/.scripts/backup.sh`
As opposed to:
`ExecStart=/home/user/.scripts/backup.sh`
I'm not sure why. Perhaps `fish`. On the other hand, I have a... |
7634555 | GetType used in PowerShell, difference between variables | 124 | 2011-10-03 11:47:40 | <p>What is the difference between variables <code>$a</code> and <code>$b</code>?</p>
<pre><code>$a = (Get-Date).DayOfWeek
$b = Get-Date | Select-Object DayOfWeek
</code></pre>
<p>I tried to check</p>
<pre><code>$a.GetType
$b.GetType
MemberType : Method
OverloadDefinitions : {type GetType()}
TypeNameOfValue... | 549,565 | 179,748 | 2022-02-19 14:06:24 | 7,634,645 | 224 | 2011-10-03 11:57:21 | 82,484 | 2016-01-22 13:05:45 | https://stackoverflow.com/q/7634555 | https://stackoverflow.com/a/7634645 | <p>First of all, you lack parentheses to call GetType. What you see is the MethodInfo describing the GetType method on [DayOfWeek]. To actually call GetType, you should do:</p>
<pre><code>$a.GetType();
$b.GetType();
</code></pre>
<p>You should see that <code>$a</code> is a [DayOfWeek], and <code>$b</code> is a custom... | <p>First of all, you lack parentheses to call GetType. What you see is the MethodInfo describing the GetType method on [DayOfWeek]. To actually call GetType, you should do:</p> <pre><code>$a.GetType(); $b.GetType(); </code></pre> <p>You should see that <code>$a</code> is a [DayOfWeek], and <code>$b</code> is a custom... | 526, 24067 | powershell, powershell-2.0 | <h1>GetType used in PowerShell, difference between variables</h1>
<p>What is the difference between variables <code>$a</code> and <code>$b</code>?</p>
<pre><code>$a = (Get-Date).DayOfWeek
$b = Get-Date | Select-Object DayOfWeek
</code></pre>
<p>I tried to check</p>
<pre><code>$a.GetType
$b.GetType
MemberType ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 918 | bash | # GetType used in PowerShell, difference between variables
What is the difference between variables `$a` and `$b`?
```
$a = (Get-Date).DayOfWeek
$b = Get-Date | Select-Object DayOfWeek
```
I tried to check
```
$a.GetType
$b.GetType
MemberType : Method
OverloadDefinitions : {type GetType()}
TypeNameOfValue... | First of all, you lack parentheses to call GetType. What you see is the MethodInfo describing the GetType method on [DayOfWeek]. To actually call GetType, you should do:
```
$a.GetType();
$b.GetType();
```
You should see that `$a` is a [DayOfWeek], and `$b` is a custom object generated by the [Select-Object](http://t... |
13588457 | Forward function declarations in a Bash or a Shell script? | 117 | 2012-11-27 16:17:37 | <p>Is there such a thing in <code>bash</code> or at least something similar (work-around) like forward declarations, well known in C / C++, for instance?</p>
<p>Or there is so such thing because for example it is always executed in one pass (line after line)?</p>
<p>If there are no forward declarations, what should I... | 29,204 | 435,800 | 2024-10-08 14:28:49 | 13,588,485 | 224 | 2012-11-27 16:19:21 | 68,587 | 2024-10-08 14:28:49 | https://stackoverflow.com/q/13588457 | https://stackoverflow.com/a/13588485 | <p>No, bash does not have forward function declarations. I use a pattern like this for most of my scripts:</p>
<pre><code>#!/bin/bash
main() {
foo
bar
baz
}
foo() {
}
bar() {
}
baz() {
}
main "$@"
</code></pre>
<p>You can read the code from top to bottom, but it doesn't actually start executi... | <p>No, bash does not have forward function declarations. I use a pattern like this for most of my scripts:</p> <pre><code>#!/bin/bash main() { foo bar baz } foo() { } bar() { } baz() { } main "$@" </code></pre> <p>You can read the code from top to bottom, but it doesn't actually start executi... | 387, 5569, 10327, 19986 | bash, forward-declaration, function, sh | <h1>Forward function declarations in a Bash or a Shell script?</h1>
<p>Is there such a thing in <code>bash</code> or at least something similar (work-around) like forward declarations, well known in C / C++, for instance?</p>
<p>Or there is so such thing because for example it is always executed in one pass (line afte... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 919 | bash | # Forward function declarations in a Bash or a Shell script?
Is there such a thing in `bash` or at least something similar (work-around) like forward declarations, well known in C / C++, for instance?
Or there is so such thing because for example it is always executed in one pass (line after line)?
If there are no f... | No, bash does not have forward function declarations. I use a pattern like this for most of my scripts:
```
#!/bin/bash
main() {
foo
bar
baz
}
foo() {
}
bar() {
}
baz() {
}
main "$@"
```
You can read the code from top to bottom, but it doesn't actually start executing until the last line. By passing ... |
66963068 | Docker Alpine executable binary not found even if in PATH | 91 | 2021-04-06 05:18:18 | <p>I have an alpine running container which contains some binaries in <code>usr/local/bin</code></p>
<p>When I <code>ls</code> the content of <code>usr/local/bin</code> I got this output :</p>
<pre class="lang-sh prettyprint-override"><code>/usr/local/bin # ls
dwg2SVG dwg2dxf dwgadd dwgbmp dwgfilter ... | 133,761 | 13,000,695 | 2024-04-09 04:30:30 | 66,974,607 | 224 | 2021-04-06 18:40:07 | 7,256,341 | 2022-07-27 16:05:07 | https://stackoverflow.com/q/66963068 | https://stackoverflow.com/a/66974607 | <p>On Alpine Linux, the <code>not found</code> error is a typical symptom of dynamic link failure. It is indeed a rather confusing error by musl's <code>ldd</code> linker.</p>
<p>Most of the world Linux software is linked against <a href="https://www.gnu.org/software/libc/" rel="noreferrer">glibc</a>, the GNU libc libr... | <p>On Alpine Linux, the <code>not found</code> error is a typical symptom of dynamic link failure. It is indeed a rather confusing error by musl's <code>ldd</code> linker.</p> <p>Most of the world Linux software is linked against <a href="https://www.gnu.org/software/libc/" rel="noreferrer">glibc</a>, the GNU libc libr... | 58, 390, 90304, 115689 | alpine-linux, docker, linux, shell | <h1>Docker Alpine executable binary not found even if in PATH</h1>
<p>I have an alpine running container which contains some binaries in <code>usr/local/bin</code></p>
<p>When I <code>ls</code> the content of <code>usr/local/bin</code> I got this output :</p>
<pre class="lang-sh prettyprint-override"><code>/usr/local/b... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 920 | bash | # Docker Alpine executable binary not found even if in PATH
I have an alpine running container which contains some binaries in `usr/local/bin`
When I `ls` the content of `usr/local/bin` I got this output :
```
/usr/local/bin # ls
dwg2SVG dwg2dxf dwgadd dwgbmp dwgfilter dwggrep dwglayers dwg... | On Alpine Linux, the `not found` error is a typical symptom of dynamic link failure. It is indeed a rather confusing error by musl's `ldd` linker.
Most of the world Linux software is linked against [glibc](https://www.gnu.org/software/libc/), the GNU libc library (libc provides the standard C library and POSIX API). M... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.