Tag Info

Hot answers tagged

22

With GNU, FreeBSD or OS/X date (or date implementations that use the system's libc's strftime() where that is the GNU libc), adding hyphen - after % prevents numeric fields from being padded with zeroes: $ date +'%Y%-m%d' 2015120 From man date on a GNU system: By default, date pads numeric fields with zeroes. The following optional flags may ...


12

When you launch a terminal it will always run some program inside it. That program will generally by default be your shell. On OS X, the default shell is Bash. In combination that means that when you launch Terminal you get a terminal emulator window with bash running inside it (by default). You can change the default shell to something else if you like, ...


10

read reads from standard input. But the standard input of the bash process is already taken by the script. Depending on the shell, either read won't read anything because the shell has already read and parsed the whole script, or read will consume unpredictable lines in the script. Simple solution: bash -c "$(wget -O - http://example.com/my-script.sh)" ...


6

If you change the file owner using chown, the permissions for alice would be transferred to bob. So here's the flow: sudo mv ~bob/Documents ~bob/Documents.orig sudo mv ~alice/Documents/ ~bob/Documents sudo chown -PR bob ~bob/Documents Edit: In case you want to overwrite the group as well, use sudo chown -PR bob:bob ~bob/Documents Or: sudo chown -PR ...


6

Just use the standard sh (POSIX and Bourne) syntax: case $answer in a|A) echo OK;; *) echo >&2 KO;; esac Or: case $answer in [aA]) echo OK;; *) echo >&2 KO;; esac With bash, ksh or zsh (the 3 shells that support that non-standard [[...]] syntax), you can declare a lower case variable: typeset -l test printf 'Enter test: ' ...


5

You can remove file with name like !!, just escape it: rm \!\! or just rm !<TAB> -> rm \!\!


5

Short answer: see BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!. Long answer: the shell does variable expansion partway through the process of parsing a command line -- notably, after it processes quotes and escapes. As a result, putting quotes and escapes in a variable doesn't do the same thing as having them ...


5

There are several useful ways to achieve this (in bash): two checks echo -n "Enter test: " read test if [[ $test == "a" || $test == "A" ]]; then echo "worked" else echo "failed" fi make the input lower case echo -n "Enter test: " read test test="${test,,}" if [[ $test == "a" ]]; then echo "worked" else echo "failed" fi regex for both ...


5

TL;DR bash opens and truncates all involved files before anything is written to them. stdout and stderr are both sent to new because bash has already truncated the file (twice) when ls starts printing. This is how bash prepares/handles I/O redirection. When you ask for a command to be redirected (>) to a file, bash basically opens that file, creating it ...


4

As Bob: mv ~bob/Documents ~bob/Documents.orig cp --remove-destination --no-preserve=ownership -r ~alice/Documents/ ~bob/ sudo rm -fr ~alice/Documents/


4

Note that you don't have to read the file beforehand, sed has the r command that can read a file: $ printf -v var "%s\n" "s1random stuff" "s2 more random stuff" "s1 final random stuff" $ echo "$var" s1random stuff s2 more random stuff s1 final random stuff $ sed '/^s2/r file.txt' <<< "$var" s1random stuff s2 more random stuff line 1 line 2 s1 ...


4

Arrays in bash are defined like: a=(foo bar baz) Or: a=([12]=foo [5]=bar) (arrays in bash are more like associative arrays with keys limited to positive numbers and with elements sorted on those numerically). To delete the last character of the element with the greatest key, with recent versions of bash, you'd do: a[-1]=${a[-1]%?}


4

If nothing matches the pattern you specified (*ou*) then that is passed unexpanded to the command: $ echo *ou* *ou*


4

With GNU grep if built with PCRE support and assuming $string doesn't contain \E, you can do: grep -P "^\Q$string"


4

The exact mechanism depends on what shell is running in the "terminal session". For the BASH shell, the man page for "bash" says: MAILCHECK Specifies how often (in seconds) bash checks for mail. The default is 60 seconds. When it is time to check for mail, the shell does so before displaying the primary prompt. ...


4

You must not forget to call ls: alias ls='echo "Hello World!"; ls'


3

First you may check whether ssh-agent is running and start it if not: if ! [ -n "$SSH_AUTH_SOCK" ] || ! { ssh-add -l &>/dev/null; rc=$?; [ "$rc" -eq 0 ] || [ "$rc" -eq 1 ];}; then echo "Starting agent..." eval "$(ssh-agent -s)" fi ssh-add -l exits with code 1 if there are no identities and with code 2 if it cannot connect to ssh-agent. ...


3

The idea of ssh-agent is to have a running service storing all your keys. Therefore you only need to enter your password once and you can even forward your agent to a remote host if you want to log in to a second host from there. First verify that ssh-agent is running using ssh-add -l, which probably says "The agent has no identities." Second you add keys ...


3

for((i=2;i<=$#;i++)); do wc "${!i}" done


3

> date +'%Y %m %d' | ( read year month day; echo "${year}${month#0}${day}" ) 2015120


3

There are two sides to the question, the technical side and the historical side. The technical answer is because bash uses GNU Readline. In readline Control-a is bound to the function beginning-of-line, you can show this with: $ bind -q beginning-of-line beginning-of-line can be invoked via "\C-a", "\M-OH", "\M-[1~", "\M-[7~", "\M-[H". where \C-a means ...


3

2>&1 means redirection of error stream to the standard output and & character on its own doesn't have much meaning: it's waiting for you to provide a number of the file descriptor, but you are giving him a filename istead. You want to redirect into file, not a numbered file descriptor, so commmand >> /home/user/accumulate_output.log ...


3

You are missing your terminal is already running bash (or another shell interpreter) in the first place. A terminal and more precisely a terminal emulator in your case is just a device passing keystrokes to an underlying program and displaying whatever characters are sent to it. While it runs a shell by default, nothings forbids to start a terminal running ...


3

advanced cp cp -r /home/username/A/. /usr/lib/B/ This is especially great because it works no matter whether the target directory already exists. shell globbing If there are not too many objects in the directory then you can use shell globbing: mkdir -p /usr/lib/B/ shopt -s dotglob cp -r /home/username/A/* /usr/lib/B/ rsync rsync -a ...


3

If on a GNU system, from man cp: -T, --no-target-directory treat DEST as a normal file This allows you to write cp -rT /home/username/A/ /usr/lib/B/ to do exactly the right thing.


2

You can define a function anywhere the shell is expecting a command, including in a function. Note that the function is defined at the moment the shell executes its definition, not when the shell parses the file. So your code won't work if the user chooses option 1 the first time update_profile is executed, because when update_name is called in the case ...


2

You can also use gnu stow, a symlink farm manager. Assume the following layout: . ├── drive │   ├── a │   │   ├── b │   │   │   └── bar │   │   └── c │   │   └── baz │   └── b └── music └── a └── b └── foo Execute: $ stow --target music --dir drive . Result: . ├── drive │   ├── a │   │   ├── b │   │   │   └── bar │   │   └── ...


2

cp -avv /home/alice/Documents/ /home/bob/newDocuments/ && chown -Rvv bob:bob /home/bob/newDocuments/


2

I guess my question is where are the configuration files for tab-completion located on an Ubuntu system? In Ubuntu, the bash-completion library lives in /usr/share/bash-completion/bash_completion. When you start a shell, this library can get sourced in a number of ways, e.g., ~/.bashrc -> /etc/bash_completion -> /usr/share/bash-completion/bash_completion ...


2

for f do exec <"$f" : handle stdin done A non-interactive shell will treat any redirection from a file that cannot be read or that does not exist when associated w/ a special builtin as a fatal error and exit immediately with a meaningful diagnostic message written to stderr. So either your parameters are valid, readable files and the above ...



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