Hot answers tagged

42

To find out about a key binding. In bash: $ bind -p | grep -a '{' "\e{": complete-into-braces "{": self-insert $ LESS='+/complete-into-braces' man bash complete-into-braces (M-{) Perform filename completion and insert the list of possible com‐ pletions enclosed within braces so the list is available to the shell (see ...


22

History and history expansion are disabled by default when the shell run non-interactively. You need: #!/bin/bash set -o history set -o histexpand ls /bin ls !$ or: SHELLOPTS=history:histexpand bash script.sh it will affect all bash instances that script.sh may run.


10

One of my favourite utilties is namei, part of util-linux and hence generally present only on Linux: $ namei /usr/share/foo/bar f: /usr/share/foo/bar d / d usr d share foo - No such file or directory But its output is not very parseable. So, if you just wish to point out something is missing, namei might be useful. It's useful for troubleshooting ...


9

You might want to look into setting a DEBUG trap, which allows you to set up what is effectively a pre-exec hook in a manner similar to zsh. See http://superuser.com/questions/175799/does-bash-have-a-hook-that-is-run-before-executing-a-command.


8

Instead of &&, which doesn't exist in fish, use ; and the command and: cd /study; and ls -la According to the fish tutorial: Unlike other shells, fish does not have special syntax like && or || to combine commands. Instead it has commands and, or, and not.


6

Drop the gnuplot into a subshell and then it's the last command executed. You also no longer require the last cd because the change of directory at the beginning of the subshell affects only the gnuplot, and so the redirection to /dev/null is also moot. ( cd some/path; gnuplot -e gnuplot_file.gp ) Perhaps you intended the redirection to /dev/null to apply ...


5

Your problem is that $(git_prompt) is evaluated to some constant string before it is added to $PS1. You have to add the code instead: PS1+='$(git_prompt)'


5

The sane thing to do would be ls /bin ls $_ or set ls /bin $* $* or c='ls /bin' $c $c Caveats: it's worth remembering that each of these comes with some pitfalls. The $_ solution only captures the last single argument: so ls foo bar will leave $_ containing just bar. The one using set will override the arguments ($1, $2, etc). And all of these as ...


5

Having your .bash_profile call lastlog -u "$USER" gets you something pretty close. Output looks like: Username Port From Latest anthony pts/7 192.168.XX.YY Sun Feb 7 16:00:40 -0500 2016 where of course I redacted the IP address. last -w -n 1 gets a similar record, but from a different database.


5

One way would be to add the following to ~/.ssh/rc, which contains commands to be run when you ssh into the machine: lastlog -u $USER | perl -lane 'END{print "Last login: @F[3..6] $F[8] from $F[2]"}' The command will get the time of your last login from lastlogin and then format it so that it looks like the original version. You can now touch ~/.hushlogin ...


4

Go's idea of channels can be used here: mkfifo my_pipe exec 3<>my_pipe trap 'rm my_pipe' EXIT printf a >&3 function _abc { ... _check & } function _check { until read -N1 -u3; do :; done ... printf a >&3 } Essentially: Create a named pipe for communicating between instances of _abc and _check. Have _check ...


4

Inside the script, simply export LC_ALL=C.UTF8 at the beginning (just after the shebang line, if any). Then, all commands executed by the script will inherit LC_ALL. If you need part of your script to be immune to locale changes, but part to respect the locale (for instance, if you are to calculate and then print some values), you might need to unset ...


4

Your output shows that sync-samuel issues a sudo prompt, even though you run it without sudo and the script itself doesn’t invoke sudo.  This doesn’t make any sense.  It looks like, when you type sync-samuel, you’re running something other than the sync-samuel script that you show in the question. It is possible that sync-samuel is actually an alias for ...


4

With sed: sed -n '1p;$p' file Suppress automatic printing of pattern space (-n) but print first (1p) and last line ($p) of pattern space. If you want to edit your file "in place" use sed's option -i.


4

Use head and tail, one after another: head -n 1 file.txt; tail -n 1 file.txt Running tail if head succeeds: head -n 1 file.txt && tail -n 1 file.txt Example: % cat file.txt foo spam egg bar % head -n 1 file.txt; tail -n 1 file.txt foo bar % head -n 1 file.txt && tail -n 1 file.txt foo bar


3

There's a missing semicolon: if [ "$PS1" ]; then PS1="[\e[0;36m]\W\n[\e[m][\e[1;31m]\$[\e[m]" fi should be if [ "$PS1" ]; then PS1="[\e[0;36m]\W\n[\e[m][\e[1;31m]\$[\e[m]"; fi or, format it the same way as your other if statements above this one.


3

One way is to use a function instead of the alias - put this in your .bashrc or .bash_profile - sreq() { ffmpeg -f x11grab -r 24 -s 1024x768 -i :0.0 -qp 0 -c:v libx264 -preset veryslow $1.mkv }


3

My crystal ball thinks that maybe your message text contains one of more of < or >. It looks to me like your usage mailCommand=`mail -s info@redearmedia.ca < $email` will not do what you want: this will take the content of $email as a filename (failing somewhat because $email consists of several words), try to read its contents, pu tthose into ...


3

Instead of du -b, may I suggest you use find with printf. The main problem here is that du will recurse into any directories it finds. Your for loop is unable to see the individual files. find /home/cloudera/Desktop/new -type f -printf "%s %p\n" If your find doesn't have printf, then use -exec stat -c "%s %n" {} \; Then pipe the output to a while ...


3

The chsh command only lets you change your login shell from a shell that's listed in /etc/shells, to a shell that's listed in /etc/shells. This is a security and safety feature: if an account has a restricted shell (not listed in /etc/shells), they can't upgrade their access by switching to another shell; and a user can't lock themselves out by switching to ...


3

When sourcing the script, your current shell executes the commands. In zsh, you must use a single = in comparison. $ echo '[ a == a ]' | zsh zsh: = not found exit code: 1 $ echo '[ a = a ]' | zsh $


2

I'm concerned about 2 points PID files which I'm not familiar with, but I would suggest using pgrep as workaround. ds4drv seems a daemon but udev supports only short running processes. RUN{type} ... This can only be used for very short-running foreground tasks. Running an event process for a long period of time may block all further events ...


2

In ~/.bashrc you can add shopt -s histappend


2

You can define a trap function to capture any error that is occurring in the script. #!/usr/bin/ksh trap errtrap function errtrap { es=$? echo "`date` The script failed with exit status $es " | $log } rest of the script follows. The TRAP will capture any error at any command and will call the errtrap function. For better usage you can make the ...


2

It may be that somehow the BASH-shell-option interactive_comments was disabled. You can replicate the behavior with: ~ $ shopt -u interactive_comments ~ $ # # command not found ~ $ You can determine if it's on or off : $ shopt interactive_comments If the output is "on", then this is not the source of your problem. If it is "off", then you should grep ...


2

That error means that you have an escaped # (this means \#) in one of bash's initialization files. Since it doesn't seem to be in ~/.bashrc or ~/.bash_profile, it has to be in one of the other files bash reads when it is loaded. To be sure, just search through all of them1: grep -FH '\#' ~/.bashrc ~/.profile ~/.bash_profile ~/bash.login \ ...


2

for f in ~/1/2/*/*_1.txt; do file_without_path="${f##*/}" exptool input1= "${file_without_path}" input2= "${file_without_pathf%_1.txt}_2.txt" done


2

Bash has the concept of assigning a function to ps1 so mine looks like export PROMPT_COMMAND='PS1=$(make_ps1); set_xterm_title' where make_ps1 is make_ps1() { if [ $? = 0 ];then echo '\[\e[${host_color}m\][\D{%F %T} \u@\h \W]\[\e[0m\]\n\$ ' else echo '\[\e[7m\e[${host_color}m\][\D{%F %T} \u@\h \W]\[\e[0m\]\n\$ ' fi ...



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