11 ms·
Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
- mgr86 4y ago> Ctrl + x + Ctrl + e : launch editor defined by $EDITOR to input your command. Useful for multi-line commands. -- Great, I was just trying to remember that key combination the other day. Just got back to work after being for awhile for a child bonding leave.
- user3939382 4y agoIs that Ctrl+x, Ctrl+e? Or Ctrl+x+e? Or both controls?
- ogdrhr 4y agoall these work for me - C-x, C-e (separately) - C-(x,e) (hold ctrl, x and e separately) - C-(x+e) (hold ctrl-x and press e)
- mdaniel 4y agoit's c-x followed by c-e although no need to release the control key that I'm aware of. The c-x is in the same family as alt-x (sometimes called "meta-x") which is a similar Emacs "mode switching" leader keystroke You can read about the gory details in "man 3 readline": https://manpages.ubuntu.com/manpages/jammy/en/man3/readline.3readline.html https://manpages.ubuntu.com/manpages/jammy/en/man3/readline....
- ttyprintk 4y agoIf your $VISUAL is overwrought with plugins (ahem) you can set $FCEDIT to something lighter.
- hamosch 4y agoLearnt a neat trick from an old sysadmin colleague. If you’ve written a command but realize you don’t want to run it right now but want to save it in your history you can just put a `#` in front of it (ctrl-a #) making it a comment and allowing you to save it in your history without running it. When you’re ready to run it you find it and remove the preceding `#`
- archielc 4y agoThe opposite is adding space before the command. The command will run but it will not be saved in history. EDIT: This apparently needs to be configured - setting HISTCONTROL=ignorespace
- pseudostem 4y agoThanks to your comment, I learnt about ignoredups as well
- matijs 4y agoAnd `ignoreboth` to combine the two.
- KMag 4y agoI had been in the habit of symlinking ~/.bash_history to /dev/null to avoid AFS/NFS writes on every local command execution. When I moved over to the financial industry, it didn't occur to me that such a symlink might look like an attempt to evade monitoring. A year or two in, I realized it didn't look good, but it had clearly been made my first week on the job, so I just left it in place for over 10 years rather than risk looking like I was again monkeying with my history. I hope and presume they had much better monitoring than scanning bash history, but I'm not bet-my-career confident of that.
- JetSpiegel 4y agoIt's simpler to use a tmpfs for this purpose. $XDG_RUNTIME_DIR is already available, on modern Linux versions.
- ttyprintk 4y agosystemd-tmpfiles can be configured to delete a path upon ‘systemd-tmpfiles —-user clean’
- dijit 4y ago
- archielc 4y agoThat's a nice and very extensive collection. As a follow-up, I can also recommend Effective Shell series. I used to have navigation shortcut diagram from Part 1 (https://dwmkerr.com/effective-shell-part-1-navigating-the-command-line/ https://dwmkerr.com/effective-shell-part-1-navigating-the-co...) printed out.
- bfm 4y agoThanks for sharing, I love the animations and diagrams.
- hn_user82179 4y agoThank you so much for the navigation shortcut link! I love these and picked them up from mentors at jobs but never found a definitive guide to all of the ones I could learn.
- tambourine_man 4y agoSomething I’ve been wanting to do but haven’t found a perfect solution for yet: storing the output of previous command in some variable by default. I use Terminal.app’s Select Between Marks or tmux, but I wish this was a thing.
- quesera 4y agoYou can use: VAR=$(!!) to accomplish something similar. Not by default of course. It re-runs the command, so if not idempotent/etc it will not return expected results. Also when re-run, the command will not be in a tty context, so if the executable is sensitive to such things (e.g. `ls`), the output format might be different.
- tambourine_man 4y agoYeah, I know, but I don’t want to re-run and I want it by default.
- oh_sigh 4y agoYou could put that line in your PS1, I think?
- yakshaving_jgt 4y agoPerhaps you could change your prompt so that everything is wrapped with tee. Interesting rabbit hole to go down… https://unix.stackexchange.com/q/562018 https://unix.stackexchange.com/q/562018
- tambourine_man 4y agoI saw that too, but the amount of stuff that can go wrong is crazy. Thanks though
- kjellsbells 4y agomight be kludgey but i wonder if bash coproc would help, it basically can send stdout to a named pipe that makes it avail as a environment variable fike descriptor.
- dangom 4y agoGreat training dataset for copilot.
- bfm 4y agoNot one-liners, but some of the tools that I have found helpful for working with large bash codebases are: - shellcheck and shfmt for linting and formatting - sub for organizing subcommands - bashdb for debugging scripts interactively via the VSCode extension I'm still missing a way to share modules with others like it can be done with ansible/terraform, but I have not found an optimal way to do it yet. [shellcheck] https://github.com/koalaman/shellcheck https://github.com/koalaman/shellcheck [shfmt] https://github.com/mvdan/sh https://github.com/mvdan/sh [sub] https://github.com/qrush/sub https://github.com/qrush/sub [bashdb] http://bashdb.sourceforge.net/ http://bashdb.sourceforge.net/ [vscode-bash-debug] https://github.com/rogalmic/vscode-bash-debug https://github.com/rogalmic/vscode-bash-debug
- ttyprintk 4y agoThe bash customizations for fzf include a Ctrl-t interactive path completion. Works wonders on massive codebases.
- mdaniel 4y agosed -i Watch out, that's a Linux-ism and macOS's sed will cheerfully use the thing after it as the backup expression; as far as I know, the absolute safest choice is to always specify a backup extension "sed -i~" or "sed -i.bak" to make it portable, although there are plenty of work-arounds trying to detect which is which and "${SED_I} -e whatever" type silliness My contribution (and yeah, I know, PR it ...) is that I get a lot of mileage out of setting the terminal title from my scripts: title() { printf '\033]0;%s\007' "$*"; } and there's one for tmux, too printf '\033]2;%s\007' "$*"; with the two infinitely handy resources: * https://www.xfree86.org/current/ctlseqs.html https://www.xfree86.org/current/ctlseqs.html * https://iterm2.com/documentation-escape-codes.html https://iterm2.com/documentation-escape-codes.html
- cglong 4y agoYeah please do open PRs for these!
- gnubison 4y agoEven better, use a real text editor like ed or ex. (Nowadays ex is more portable because many distros — against POSIX — omit all 55 kilobytes of GNU ed. Of course, smaller systems might not have ex/vi.) Basic usage looks like this: printf '%s\n' '" some commands...' 'wq' | ex -s file Or: ex -s file <<'EOF' " some commands... wq EOF By the way, these commands are the ones that you use in your vimrc or after a colon in vim — at least, the POSIX subset of that — so any ex commands you learn translate naturally to your normal editor.
- mdaniel 4y agoThat's an interesting trick, I'll bear it in mind. That said, the "lottery factor" is often a bigger contributor to the things that land in codebases than "optimality". Plus, I've actually seen somewhere that perl is the most common binary across every system, and it's likely a larger population who know perl than ed would be my guess
- gnubison 4y ago
- abetusk 4y agoI love these one-liners. It's also about knowing your tools better. I hadn't known about `look` [0], which is great. The writer looks to be a bioinformatician, so it might be a bit out of scope, but I also found `socat` [1] quite a good serial communication helper tool. [0] https://man7.org/linux/man-pages/man1/look.1.html https://man7.org/linux/man-pages/man1/look.1.html [1] https://linux.die.net/man/1/socat https://linux.die.net/man/1/socat
- mxuribe 4y agoI don't recall where I heard it, but my understanding is that socat is the sort of successor to good ol' netcat. (Of course, don't ask me to compare each, nor know what socat brings that netcat lacks, etc.)
- mdaniel 4y agoAnd, fun fact, "socat" is how "kubectl port-forward" works so it'll likely be present on any machine behaving as a kubernetes Node
- adastra22 4y agoI've used `look` for years when I'm not sure how to spell some obscure word and I'm in a context where there isn't a built-in spellchecker (e.g. editing source code). I was today years old when I learned that looking up words from the system dictionary is just the convenient default and you can use it to search lines from any file.
- arittr 4y agothis is awesome, great work. this is really the first time i've opened up one of these "awesome list of X" repos and immediately learned a ton. required reading for the bash newbie and mage alike. 10/10 will reference again and again.
- chasil 4y agoNew users on my systems commonly ask me "what implements your pps process search?" When the shell itself filters the output of ps, then removing a grep is unnecessary. Note this uses POSIX shell patterns, not regular expressions. On a truly POSIX shell that does not support "local," remove the keyword, and change the braces to parentheses to force the function into a subshell. pps () { local a= b= c= IFS='\0'; ps ax | while read a do [ "$b" ] || c=1; for b; do case "$a" in *"$b"*) c=1;; esac; done; [ "$c" ] && printf '%s\n' "$a" && c=; done; } $ pps systemd PID TTY STAT TIME COMMAND 1 ? Ss 5:11 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 557 ? Ss 0:19 /usr/lib/systemd/systemd-journald ...
- jwilk 4y agoIFS='\0' doesn't do what it seems: https://github.com/koalaman/shellcheck/wiki/SC2141 https://github.com/koalaman/shellcheck/wiki/SC2141 You almost always want "read -r": https://github.com/koalaman/shellcheck/wiki/SC2162 https://github.com/koalaman/shellcheck/wiki/SC2162
- chasil 4y agoThanks for the critique. pps () { local a= b= c= IFS=$'\0'; ps ax | while read -r a do [ "$b" ] || c=1; for b; do case "$a" in *"$b"*) c=1;; esac; done; [ "$c" ] && printf '%s\n' "$a" && c=; done; }
- archduck 4y agoI was surprised to see `$()` missing from this (otherwise quite extensive) list. There are a few commands listed which employ it, but it absolutely deserves its own entry. That and `readlink -f` to get the absolute path of a file. (Doesn't work on MacOS; the only substitute I've found is to install `greadlink`.) And `cp -a`, which is like `cp -r`, but it leaves permissions intact - meaning that you can prepend `sudo` without the hassle of changing the ownership back. I never see `lndir` on these lists either. It makes a copy of a directory, but all of the non-directory files in the target are replaced with symlinks back to the source while directories are preserved as-is. Meaning that when you `cd` into it, you are actually landing in a copied structure of the source directory instead of the source directory itself, as would be the case if you just symlinked the source folder. Once inside, any file you want to modify without affecting the original just needs you to create the symlink into a file, which you can do with `sed -i '' $symlink`. There you have it: effectively a copy of your original directory, with only the modified files actually taking up space (loosely speaking). Looks like I have a few pull requests to submit.
- hummusFiend 4y agoFWIW `realpath` on macOS should be functionally equivalent to `readlink -f` - particularly if you ignore all the other functionality `readlink` provides.
- archduck 4y agoNice, thank you! I did not know about `realpath`. Piping absolute paths and copying them to the clipboard is a pattern I use a lot, so this tip will get substantial mileage out of me.
- ineedasername 4y ago>a copy of your original directory, with only the modified files actually taking up space What's the use case for this sort of thing?
- gizmo686 4y ago
- teddyh 4y ago> $SHELL current shell No, $SHELL is the user’s default shell; i.e. the shell started in a new terminal or when logging in on a console or remotely. If another shell program is started, $SHELL will still refer to the default shell, not the running shell program.
- superasn 4y agoThis is a rather long list so i'll just mention my latest favorite which I recently learned on HN and it has made my life very easy with bash Whenever you need to use a single-quote on command line add a $ sign before it. It makes escaping everything super easy su user -c $'cd \'$dir\' && ...' Before this it used to confuse the hell out of me. More details are here: https://stackoverflow.com/a/16605140/1031454 https://stackoverflow.com/a/16605140/1031454
- mdaniel 4y agoI know that may be convenient, but you'll want to exercise caution because $'' turns on inner escaping that wouldn't otherwise happen inside single-quoted strings $ echo 'hello \"world' hello \"world $ echo $'hello \"world' hello "world
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- a_square_peg 4y agoThis is a fantastic list - thank you for sharing.
- asicsp 4y agoSee also "The Art of Command Line": https://github.com/jlevy/the-art-of-command-line https://github.com/jlevy/the-art-of-command-line
- hiisukun 4y agoMy favourite example in this page is used four times but not pointed out specifically. It's the use of <(some stuff) to create a temporary file descriptor. Since it's not explained I'll give it a quick go. If you ever found yourself doing something like this: sort file1 > file1_sorted sort file2 > file2_sorted diff file1_sorted file2_sorted You can instead skip making two new files, and do: diff <(sort file1) <(sort file2) And voila - you've got two 'files' you are comparing, but without having to save them to disk. The examples on the page use this with `curl` and `head` to good effect, but it wouldn't necessarily be obvious what's going on.
- swah 4y agoRelated: the Warp terminal has the concept of "workflows" - which are a list of snippets you can pull and use with auto-complete. I found that to be a good way to remember those one-liners that I only use once every two months.
- mekster 4y agoCtrl + s : to stop output to terminal. Ctrl + q : to resume output to terminal after Ctrl + s. Who uses these? These are newb as well as advanced user killers that doesn't seem to serve a purpose. It just makes you think your shell is locked up making you think either the server is out of memory, process is hung to the point no keys would react or the network is hosed.
- michaelcampbell 4y agoI /remember/ them from when I was logging in to "the computer lab" at my Uni from literally decades ago, but I haven't thought about them nor used them in at least that time.
- ttanev 4y agoA really nice list, thanks a lot for sharing! :) I'd add this when a filesystem gets almost full (but not overfilled, see below). This shows where most of the space goes: # du -axm / | sort -n | tail # takes a while on large filesystems, or ones with lots of files Then narrow down for each of the most filled directories: # du -axm /some/dir | sort -n | tail # subsequent searches are fast, now that metadata is cached. In case there is no space at all, sort will complain if the /tmp directory is on the same fs, then the only option is to search any suspect directories with du -sm $dir And about this one: https://github.com/onceupon/Bash-Oneliner#using-ctrl-keys https://github.com/onceupon/Bash-Oneliner#using-ctrl-keys A bit surprised that the Ctrl+b(ack one...) and Ctrl+f(orward one char) shortcuts are not included. As well as their Alt+b/f for a word back/forward too. Very convenient for going through a long command by getting in the beginning or the end of the line, then move words back/forth to update it.