Home Explore Blog CI



nushell

5th chunk of `book/coming_from_bash.md`
558ab5af1a9c8937ad010b83d6019b8749177d4dc3cab0b70000000100000e0d
| `bash <script file>`                 | `nu <script file>`                                            | Run a script file                                                 |
| `\`                                  | `( <command> )`                                               | A command can span multiple lines when wrapped with `(` and `)`   |
| `pwd` or `echo $PWD`                 | `pwd` or `$env.PWD`                                           | Display the current directory                                     |
| `read var`                           | `let var = input`                                             | Get input from the user                                           |
| `read -s secret`                     | `let secret = input -s`                                       | Get a secret value from the user without printing keystrokes      |

## History Substitutions and Default Keybindings:

| Bash                                 | Nu                                                            | Task                                                              |
| ------------------------------------ | ------------------------------------------------------------- | ----------------------------------------------------------------- |
| `!!`                                 | `!!`                                                          | Insert last command-line from history                              |
| `!$`                                 | `!$`                                                          | Insert last spatially separated token from history                |
| `!<n>` (e.g., `!5`)                  | `!<n>`                                                        | Insert \<n\>th command from the beginning of the history          |
|                                      |                                                               | Tip: `history \| enumerate \| last 10` to show recent positions   |
| `!<-n>` (e.g., `!-5`)                | `!<-n>`                                                       | Insert \<n\>th command from the end of the history                |
| `!<string>` (e.g., `!ls`)            | `!<string>`                                                   | Insert the most recent history item that begins with the string   |
| <kbd>Ctrl/Cmd</kbd>+<kbd>R</kbd>     | <kbd>Ctrl/Cmd</kbd>+<kbd>R</kbd>                              | Reverse history search                                            |
| (Emacs Mode) <kbd>Ctrl</kbd>+<kbd>X</kbd><kbd>Ctrl</kbd>+<kbd>E</kbd> | <kbd>Ctrl/Cmd</kbd>+<kbd>O</kbd> | Edit the command-line in the editor defined by `$env.EDITOR`   |
| (Vi Command Mode) <kbd>V</kbd>       | <kbd>Ctrl/Cmd</kbd>+<kbd>O</kbd>                              | Edit the command-line in the editor defined by `$env.EDITOR`       |

Most common Emacs-mode and Vi-mode keybindings are also available. See the [Reedline Chapter](line_editor.html#editing-mode).

::: tip
In Bash, history substitution occurs immediately after pressing <kbd>Enter</kbd> 
to execute the command-line. Nushell, however, *inserts* the substitution into
the command-line after pressing <kbd>Enter</kbd>. This allows you to confirm
the substitution and, if needed, make additional edits before execution.

This behavior extends to "Edit command-line in Editor" as well. While Bash immediately
executes the command after exiting the editor, Nushell (like other, more modern shells
such as Fish and Zsh) inserts the editor contents into the command-line, allowing you
to review and make changes before committing it to execution.
:::

Title: History Substitutions and Keybindings: Bash vs. Nushell
Summary
This section compares Bash and Nushell in terms of history substitutions and keybindings. It shows equivalent commands for inserting the last command, last token, nth command from the beginning or end of history, and the most recent command starting with a specific string. It also covers reverse history search. Further, it explains how to edit the command line in an external editor, highlighting the different keybindings in Emacs and Vi modes. A tip emphasizes the difference in how Bash and Nushell handle history substitution and editing in an editor: Nushell inserts the substituted/edited command into the command line for review before execution, unlike Bash which executes immediately.