Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Hot answers tagged

7

In ksh93: PS1='${PWD#${PWD%?/*/*/*}?/} \$ ' share/doc/libnl-3-dev $ _ PS1='[${HOSTNAME%%.*}:${PWD#${PWD%?/*/*/*}?/}] $USER% ' [host:share/doc/libnl-3-dev] user% _ If you want it to also replace $HOME with ~, something nastier is needed: PS1='$(d=${PWD/#$HOME/"~"};printf %s "${d#${d%?/*/*/*}?/}") $ ' ~/w/maemo $ cd sb2-pathmaps w/maemo/sb2-pathmaps $ _ ...


6

With shift $# you empty $@ completely. The $@ in the function is separate from the $@ in the main script. Why don't you just pass all arguments to the function and then shift off the first three? #! /bin/bash -- set -x ## docker-compose wrapper compose_fn() { local env="$1" local verb="$2" local service="$3" local cmd=( docker-compose -f "$env....


6

For bash, you could achieve similar results by setting the PROMPT_DIRTRIM variable: $ PS1='[\u@\h] \w\$ ' [schaller@r2d2] ~$ pwd /home/schaller [schaller@r2d2] ~$ PROMPT_DIRTRIM=3 [schaller@r2d2] ~$ cd /home/schaller/tmp/513924/another/directory/here [schaller@r2d2] ~/.../another/directory/here$


4

Bash doesn't yet make it easy to sort files by modification time, so here's the obligatory zsh-based answer. You don't have to switch to zsh as your login shell in order to use its features. Here I set up a wrapper function that expects either one or two arguments; the first argument is the program to execute (e.g. myprogram); the second, optional, ...


4

If you've got reasonably up-to-date versions of the GNU utilities you can have them handle NULL-terminated data. This allows one to construct pipelines that are not affected by whitespace or newlines in the data itself. My test tool is a quick script called /tmp/args: #!/bin/bash echo "This is args with $# value(s)" for f in "$@"; do echo "> $f <"; ...


3

Each shell process has its own idea of what the command-line history is. When an interactive shell exits it will write its remembered history to ~/.bash_history for the next shell to pick up, but that's the extent of cooperation between shell processes. In your command, the () makes the shell fork a copy of itself to run history -d command. The child ...


2

In a non-interactive sh, commands run asynchronously with & have their SIGINT and SIGQUIT signals ignored. That's a POSIX requirement, and goes back to the time when terminal job control (tcsetpgrp()...) didn't exist. Today, it generally gets in the way. Another POSIX requirement and that comes from the same origin is that when a signal was ignored ...


2

The easiest thing you can do is to trap the SIGINT (^C) and kill the background process from the handler: #! /bin/sh trap 'kill "$!"; exit 1' INT QUIT zenity --info & wait See this and other similar answers for an explanation for this behavior. When calling your script from some shells like bash which implement the so-called "Wait and Cooperative Exit"...


2

OK, seems there have been a few somewhat similar questions. The answer is to use: export HISTIGNORE="rm*" (It seems to be using glob syntax, and multiple patterns can be given by separating with colons :) Another way is to set: export HISTCONTROL=ignorespace which will cause Bash to keep all commands starting with a space out of the history. So if you ...


2

As noted in man sudoers: Command environment Since environment variables can influence program behavior, sudoers pro‐ vides a means to restrict which variables from the user's environment are inherited by the command to be run. There are two distinct ways sudoers can deal with environment variables. By default, the env_reset ...


2

(IFS=$'\n'; set -f; your_program $(ls -tr)) Assuming that the filenames don't contain newlines. Example: % touch 'a b' % touch 'c d' % touch '*' % (IFS=$'\n'; set -f; printf '%s\n' $(ls -tr)) e f a b c d * Variant for a standard shell which doesn't support $'...' strings: (IFS=' ' ; set -f; your_program $(ls -tr)) Simple python wrapper which ...


2

I'm not sure I understand you, but you may be looking for the fc shell builtin. FCEDIT=vi fc will let you edit in vi your last command line.


2

You have to quote your variable: echo "$QUERY" Otherwise the shell expands the * to the files in the current directory. Try echo "*" and echo * in your shell. The * is part of a feature of your shell (probably Bash) called Pathname Expansion. From the bash man page: The special pattern characters have the following meanings: * Matches any ...


2

The difference between sourcing a dot-script with source (or with ., which is the standard command) and running it as its own script has been discussed before. See e.g. "What is the difference between sourcing ('.' or 'source') and executing a file in bash?". It comes down to the difference between running a script in its own separate ...


1

If you don't need any smarts about the date structure, i.e. it will always be of the form YYYY/MM/DD or YYYY-MM-DD, you can get away with a pretty simple command that will just correct the first line of every file in the entire directory tree. find . -type f -exec sed -i '1 s:-:/:g' {} \; sed -i edits the file in-place, no need to concatenate and move ...


1

The following works for me as desired: intended_process & disown $! It seems that the $! part is optional Thanks for the suggestion @mosvy


1

Loop over the files. For each file, read the two numbers, then compare them: for file in file1 file2 file3; do { read num1; read num2; } <"$file" if [ "$num1" -gt "$num2" ]; then echo Higher elif [ "$num1" -lt "$num2" ]; then echo Lower else echo Same fi done Arithmetic comparison in the shell is done with -gt ...


1

This is definitely possible, but it's not pleasant. In reality, you'd want to go a different way about things. For these particular files and this task, this script is probably the simplest, and uses a single while loop instead of any for: exec 3<file2 while read a1 a2 a3 && read b1 b2 b3 <&3 do echo $((a1 - b1)) echo $((a2 - b2)) ...


1

When using the command shell, prefixing a command with nohup prevents the command from being aborted automatically when you log out or exit the shell. nohup mycommand & or ssh -n -f user@host "sh -c 'cd /whereever; nohup ./whatever > /dev/null 2>&1 &'" It should keep running even when you disconnect.


1

Any ideas how to fix or bypass this ? You can bypass it by turning on the posix mode, either with the --posix option, or temporarily with set -o posix: set -o posix select opt in foo bar baz; do echo "opt=$opt" done set +o posix For an explanation for this behavior, you can look at the zread() function, which is used by the read builtin (which is ...


1

If zsh is an option, then it's rather simpler: set -o nocaseglob array=( *.(png|jpg)(Om[-10,-1]) ) The set -o nocaseglob allows the simpler png|jpg to match variations in case, such as PNG or JpG. The next statement assigns an array the results of a very specific filename generation (glob). From left to right: *.(png|jpg) -- expands to the list of ...


1

In order to provide your filename as an argument to the script you need to use positional parameters You could use the following script to accomplish this: #!/bin/bash awk '{ print NF; exit }' "$1" This assumes you only care about the number of columns in the first line. If you want to find the line with the highest number of columns you could do: #!/...


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