Hot answers tagged

30 votes
Accepted

mv: rename <oldname> to <newname>/<oldname>: Invalid argument

The filesystem on your NAS is case-insensitive¹, so OldHDD and oldhdd are the same file. But your operating system is case-sensitive, so it considers those different names. The mv command asks the ...
Gilles 'SO- stop being evil''s user avatar
23 votes

What is the difference between locate txt vs locate *.txt?

locate txt locates all files (of any type including regular, symlink, directory, socket...) whose path contains txt¹, so includes /foo/xtxty/bar, /foo/bar.txt, /foo/txt.bar, etc. locate *.txt is ...
Stéphane Chazelas's user avatar
22 votes
Accepted

What does a hyphen do next to the argument position in bash shell script? Like ${1-}

A hyphen in a parameter expansion allows a default value to be specified. So ${1-} means “the value of the first parameter if it is set, and the empty string otherwise”. That doesn’t seem particularly ...
Stephen Kitt's user avatar
20 votes
Accepted

Is it now safe to parse the output of GNU ls?

Is it now safe to parse the output of GNU ls? (with --zero) --zero does help, a lot, but it's still not safe the way it was used here. There are issues with both the output format of ls itself, and ...
ilkkachu's user avatar
  • 135k
19 votes
Accepted

GNU Coreutils `\time --version` and `/bin/true --version` work but `\true --version` does not?

In Bash time is a keyword (see type time) and \time is not interpreted as such. \time makes Bash run an external executable which in your case is GNU time. But true is a builtin (see type true). The ...
Kamil Maciorowski's user avatar
18 votes
Accepted

Use the 'cp' command in a Bash script and exclude a specific directory

You should enable the extglob option, it isn't enabled by default in scripts: #!/bin/bash shopt -s extglob cp -var test/!(test2) testbkp Also verify that the script is indeed running under bash.
user000001's user avatar
  • 3,538
16 votes
Accepted

Why xargs does not process the last argument?

What went wrong b appears in the output, so it was processed, just not the way you expected. As a first step, ask bash to tell you what it sees: pass the -x option to enable its traces. $ echo a b | ...
Gilles 'SO- stop being evil''s user avatar
16 votes

For loop through servers with custom ports (for i in "user1@server1 -p 12345" "user2@server2 -p 54321" ...; do)

Since version 7.7, OpenSSH's ssh (along with its scp and sftp) will accept the destination in the form of a URI. You can specify the port number as part of the URI if you like, for example "ssh://...
Kenster's user avatar
  • 3,350
15 votes

bg command not sending process to background

From your description, the bg command did work, otherwise ctrl-z and ctrl-c would still work. Just because a process is in the background doesn't mean it can't still send you outoutput. If you don't ...
user10489's user avatar
  • 6,301
15 votes
Accepted

Does ssh run commands (rather than shell itself) in a login shell?

login shell can mean two different things in that context: the shell that is defined as the login shell for the user in the account database. For instance: $ getent passwd stephane stephane:*:1000:...
Stéphane Chazelas's user avatar
15 votes

Understanding "side effect", or multiple commands within one command?

The : is called the null command. You can find its documentation in man bash: : [arguments] No effect; the command does nothing beyond expanding arguments and performing any specified redirections....
terdon's user avatar
  • 239k
15 votes
Accepted

Running GNU screen through SSH, the shell is not a login shell?

From the man pages of GNU screen: -s program sets the default shell to the program specified, instead of the value in the environment variable $SHELL (or "/bin/sh" if not defined). This can ...
aviro's user avatar
  • 4,035
15 votes

For loop through servers with custom ports (for i in "user1@server1 -p 12345" "user2@server2 -p 54321" ...; do)

The right way would be to store that on config, rather than specifying everything on every connection. On ~/.ssh/config you could have: Host server1.domain1.com # When connecting to this User user1 #...
Ángel's user avatar
  • 3,549
14 votes

Does ssh run commands (rather than shell itself) in a login shell?

No, SSH only invokes a login shell when logging you into a shell, not when running a command. This means that if you want your login initialization files to run (e.g. ~/.profile), you need to do this ...
Gilles 'SO- stop being evil''s user avatar
14 votes
Accepted

How to referencing $@ without pass it in bash function?

You can't without A making its positional parameters available to B one way or another. Could be by passing them along: A() { B "$@" } B() { [ "$#" -eq 0 ] || printf '<%s>...
Stéphane Chazelas's user avatar
13 votes
Accepted

Why don't we use #!command for the shebang line?

A path-less shebang assumes that the command in the shebang is in the current directory, in the general case. More generically, a non-absolute shebang is interpreted relative to the current directory ...
Stephen Kitt's user avatar
12 votes
Accepted

Why does `trap` passthough zero instead of the signal the process was killed with?

$? contains the exit status of the last command that was run and waited for. You'll find that in: $ bash -c 'trap "echo \$?" INT; sleep 10; exit' ^C130 130 was reported because both sleep ...
Stéphane Chazelas's user avatar
12 votes
Accepted

How to exclude swapfile from rsync backup?

The filter file is particular about its whitespace. For example, after the leading - (dash, space) everything is a relevant character. This includes trailing space. You can check the filter file for ...
Chris Davies's user avatar
11 votes
Accepted

What does command eval "$(/opt/homebrew/bin/brew shellenv)" actually do?

$() is command substitution. It executes the command inside the parentheses and returns the output of that command. It's often used to get the output of a program into a variable. e.g. $ month=$(...
cas's user avatar
  • 77.7k
11 votes
Accepted

What is difference between these two declarations of associative arrays in Bash?

declare -A is the only reason your first declaration is treated as an associative array. Your second declaration is handled as an indexed array. When manipulating indexed arrays, indices are treated ...
Stephen Kitt's user avatar
11 votes

What does >&- do in a unix / linux terminal?

Quoting the POSIX specification for the sh language: 2.7.6 Duplicating an Output File Descriptor The redirection operator: [n]>&word shall duplicate one output file descriptor from another, or ...
Stéphane Chazelas's user avatar
11 votes
Accepted

What does mean `^(*.c|*.md)`

In Zsh, ^ is a glob operator available when EXTENDED_GLOB is set, matching anything except the following pattern. The parentheses group a pattern; this is useful in particular with disjunctions (|) ...
Stephen Kitt's user avatar
11 votes

Open PDF from a command line and go back to the command line

You don't actually need to press enter, you can use the terminal directly. What happens is that evince can print out various messages to standard error, so those make your temrinal look like it isn't ...
terdon's user avatar
  • 239k
10 votes

Is there a way to do namespaces like in C for bash?

Well, there is no namespaces in C, in C++ - yes, there are. But none in C. And no, there are no namespaces in bash either. bash has three name spaces - for local variables, functions and environment ...
White Owl's user avatar
  • 4,724
10 votes
Accepted

Why does tar's handling of stdout and - differ?

The difference in behaviour comes from tar: when writing, it applies a “blocking factor”, which by default uses 10240-byte records (that’s 2800 in hexadecimal). This happens even when compressing, ...
Stephen Kitt's user avatar
10 votes

Is it now safe to parse the output of GNU ls?

If you're going to depend on the output of GNU ls specifically, that means you're dependent on the GNU Coreutils package. That means you can instead use another Coreutils utility, namely stat. Stat ...
Kaz's user avatar
  • 7,939
10 votes

mv a bunch of files, but ask for each file

You could use the -p (prompt) option of xargs or the -ok predicate of find: find ./*.jpg -prune -ok mv {} dest/directory/ ';' printf '%s\0' *.jpg | xargs -0pI@ mv -- @ dest/directory/ (here assuming ...
Stéphane Chazelas's user avatar
10 votes
Accepted

For loop through servers with custom ports (for i in "user1@server1 -p 12345" "user2@server2 -p 54321" ...; do)

For a for loop that can loop over more than one variable, switch to zsh. You're already using zsh syntax by not quoting your variables: # zsh for colour host port ( green [email protected] ...
Stéphane Chazelas's user avatar
9 votes
Accepted

Does POSIX guarantee that all its shell utilities will resolve symbolic links where a file is expected?

POSIX does not require that all the utilities it specifies resolve any symbolic link provided as an argument expecting a file name or path. It does however document in detail how symbolic links should ...
Stephen Kitt's user avatar
9 votes

How do I keep a dash shell script running despite any error?

That's because . is a special builtin. That is a POSIX requirement. Most other POSIX shells also do it, but in the case of bash and zsh only when they're in their respective posix/sh/ksh mode (...
Stéphane Chazelas's user avatar

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