Hot answers tagged

112 votes
Accepted

Is redirection with `>>` equivalent to `>` when target file doesn't yet exist?

tl;dr No. >> is essentially "always seek to end of file" while > maintains a pointer to the last written location. Full answer (Note: all my tests done on Debian GNU/Linux 9). Another ...
Kamil Maciorowski's user avatar
69 votes
Accepted

What is wrong with “echo $(stuff)” or “echo `stuff`”?

tl;dr The sole stuff would most probably work for you. Full answer What happens When you run foo $(stuff), this is what happens: stuff gets executed, in a subshell, setting it up requires some time; ...
36 votes
Accepted

Is there a way to "source" a sh script from the fish shell?

For sourcing a general sh script in fish, one option is the bass plugin, which can source a bash script's environment variables into fish. If you have fisherman, you can install it with fisher install ...
Elliott Beach's user avatar
33 votes

How can I resume a stopped job in Linux?

If you didn't launch it from current terminal, use ps aux | grep <process name> to find the process number (pid), then resume it with: kill -SIGCONT <pid> (Despite the name, kill is ...
mahemoff's user avatar
  • 1,063
32 votes
Accepted

What does % do in Linux shell strings?

When % is used in pattern ${variable%substring} it will return content of variable with the shortest occurance of substring deleted from back of variable. This function supports wildcard patterns - ...
Marek Rost's user avatar
  • 2,076
25 votes
Accepted

Why doesn't this softlink work as expected?

That is a documented feature. If you run bash via a symlink named sh, bash will start in sh compatibility mode. From man bash: If bash is invoked with the name sh, it tries to mimic the ...
John1024's user avatar
  • 16.8k
15 votes

Why do I sometimes get 'sh: $'\302\211 ... ': command not found' in xterm/sh?

It happens when I first attempt to paste a command from the clipboard, Linux-style (Ctrl+Shift+V) and when that doesn't work, paste with the mouse. Apparently the keyboard combination inserts an ...
Mark Jeronimus's user avatar
15 votes
Accepted

What is the second sh in `sh -c 'some shell code' sh`?

Preliminary note It's quite uncommon to see sh -c 'some shell code' invoked directly from a shell. In practice, if you're in a shell then you will probably choose to use the same shell (or its ...
Kamil Maciorowski's user avatar
12 votes
Accepted

Appending new lines to multiple files

There are few issues. >> in your first command will be interpreted by your current shell as a redirection to a file literally named {}, unless it's quoted. *.ovpn may be expanded by shell ...
Kamil Maciorowski's user avatar
11 votes

How to use source command in shell script?

Please use the command: source start.sh I came across this very problem, when I wanted to avoid writing: source ~/.bashrc source activate tensorflow jupyter notebook Thereafter, I came across a ...
abhishekPakrashi's user avatar
10 votes

How do I start in bash when ssh'ing into my server?

For the case where you're trying to use a shared account (for whatever reason) and can't change the default shell, then you can run ssh -t <user@hostname> bash -l If you need to keep your ...
UKMonkey's user avatar
  • 201
9 votes

What does % do in Linux shell strings?

Bash Reference Manual: Shell Parameter Expansion ${parameter%word}${parameter%%word} The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a ...
Steven's user avatar
  • 27.8k
9 votes

How to find length of string in shell

Using ${#string} to get the length of $string is a POSIX shell parameter expansion. It is not a bash-only feature. On Solaris 5.10, if /bin/sh or /usr/bin/sh (as mentioned in the sh(1) manual) does ...
Kusalananda's user avatar
  • 2,333
9 votes

How to display current path in command prompt in linux's sh (not bash)?

This command works for me. export PS1="\u@\h: \W:$" Where \u = username \h = hostname \W Name of present folder (not full path)
High Performance Rangsiman's user avatar
9 votes
Accepted

Associating linux shell (.sh) scripts on Windows 10 to Bash or WSL

EDIT: the registry key and information below is OBSOLETE, please refer to the new version provided here: Can I drag and drop files to an .sh script using Bash on Ubuntu on Windows or Windows Subsystem ...
Alex Sim's user avatar
  • 371
8 votes

Bash script: Create a screen session and execute a command in it

You need you pass the command to run inside the screen session. You can do this by passing the command directly, or by calling a script which runs whatever command you wanted. screen -S test "...
djsmiley2kStaysInside's user avatar
7 votes

How to display current path in command prompt in linux's sh (not bash)?

One might consider to pimp the prompt by adding some colors. For instance: export PS1='\[\e[0;36m\]\u\[\e[0m\]@\[\e[0;33m\]\h\[\e[0m\]:\[\e[0;35m\]\w\[\e[0m\]\$ '
Arvid's user avatar
  • 171
7 votes

What does % do in Linux shell strings?

By experimenting, I find that a match after % is discarded, when the string is enclosed in curly brackets (braces). To illustrate: touch abcd # Create file abcd for file in ab*; do echo $...
Nissim Nanach's user avatar
7 votes

What does % do in Linux shell strings?

In Linux shell (bash), what does % do? for file in *.png.jpg; do mv "$file" "${file%.png.jpg}.jpg" done In this particular case, the % is pattern matching operator (note it can also be a modulo ...
DavidPostill's user avatar
  • 156k
7 votes
Accepted

GNU Parallel - global variables and function

Shell variables are not visible to child processes unless they are exported so you'll need to say export GLOBAL_VAR somewhere before the child process is launched; in modern shells the export can be ...
thrig's user avatar
  • 786
7 votes
Accepted

How can I use a "here document" in the middle of a pipe?

First you need to quote any part of EOF just after <<. The most natural way is <<"EOF", but <<E"OF" or even <<""EOF will do. Without this envsubst will get the string with ${...
Kamil Maciorowski's user avatar
7 votes
Accepted

What does 'x' mean in this line #!/bin/sh -x?

What x mean in the shebang #!/bin/sh -x? It allows you to debug the shell script: Bash provides extensive debugging features. The most common is to start up the subshell with the -x option, which ...
DavidPostill's user avatar
  • 156k
6 votes
Accepted

How to use source command in shell script?

source is a shell built-in command. The which command looks for binaries on the PATH, like in /usr/bin, /bin, /sbin, etc. but you won't find any built-in commands in a separate binary. Also, having ...
allquixotic's user avatar
  • 34.5k
6 votes
Accepted

using xargs pass arguments to sub shell with pipe

This does what I need it to do find ./ -name "cleanup.log-*" | xargs -I '{}' sh -c 'mv "$1" $(echo {} | cut -d"-" -f-2)' - {} The tricky bit was converting the xargs replacement -I {} string into ...
nelaaro's user avatar
  • 13.5k
6 votes

What is wrong with “echo $(stuff)” or “echo `stuff`”?

Another difference: The sub-shell exit code is lost, so the exit code of echo is retrieved instead. > stuff() { return 1 } > stuff; echo $? 1 > echo $(stuff); echo $? 0
WaffleSouffle's user avatar
6 votes
Accepted

sh source can't find files

The . (dot) command is required by POSIX, it should work in any implementation of sh. The error you got .: build.env: not found. indeed comes from .. It's build.env that is not found. It's because ...
Kamil Maciorowski's user avatar
5 votes
Accepted

Find filenames with uppercase extension

The following works for simple cases: find -name "*.*[A-Z]*" ! -name "*.*[^A-Z]*" It is liable to fail for files with two or more dots in the name. For this case you need to use Regular Expressions, ...
AFH's user avatar
  • 17.5k
4 votes
Accepted

sh syntax to handle zero files matching a wildcard, as well as more?

The simplest portable way to do this is to skip the loop for if the expansion doesn't produce something that actually exists: for f in *.ext1 *.ext2; do [ -e "$f" ] || continue handle "$f" done ...
Gordon Davisson's user avatar
4 votes
Accepted

Is it POSIX-compliant to use a shell function in a pipeline?

Yes. See this document. 1. Introduction The Shell and Utilities volume of POSIX.1-2017 describes the commands and utilities offered to application programs by POSIX-conformant systems. From 2.9.2 ...
Kamil Maciorowski's user avatar
4 votes
Accepted

Bad number error with Linux float variable value comparison operator condition

As specified in the "How can I calculate with floating point numbers instead of just integers?" post, the main point is that "BASH's builtin arithmetic uses integers only" and you are working with a ...
l0b0's user avatar
  • 7,321

Only top scored, non community-wiki answers of a minimum length are eligible