aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-20 15:53:19 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-20 15:53:39 +1200
commita143ce9c38942ebe2245ad0e7c33462087692809 (patch)
treeb86f9f8882c043cd074f6e061086f92cc653823b
parentAdd an issue about vr() (diff)
downloaddotfiles-a143ce9c38942ebe2245ad0e7c33462087692809.tar.gz
dotfiles-a143ce9c38942ebe2245ad0e7c33462087692809.zip
Port lhn() to POSIX sh
-rw-r--r--README.markdown3
-rw-r--r--bash/bashrc.d/lhn.bash7
-rw-r--r--sh/shrc.d/lhn.sh12
3 files changed, 14 insertions, 8 deletions
diff --git a/README.markdown b/README.markdown
index b1e581d5..558c8739 100644
--- a/README.markdown
+++ b/README.markdown
@@ -180,6 +180,8 @@ in `sh/shrc.d` to be loaded by any POSIX interactive shell. Those include:
information written by the `grep.sh` script in `~/.profile.d`.
* `hgrep()` allows searching `$HISTFILE`.
* `keychain()` updates `$GPG_TTY` if set for `keychain(1)`.
+* `lhn()` gets the history number of the last command, if the POSIX `fc`
+ builtin is available.
* `ls()` tries to apply color to `ls(1)` for interactive use if available.
It's dependent on information written by the `ls.sh` script in
`~/.profile.d`.
@@ -206,7 +208,6 @@ There are a few other little tricks defined for other shells, mostly in
* `fnl()` runs a command and saves its output and error into temporary files,
defining variables with the filenames in them.
* `keep()` stores ad-hoc shell functions and variables.
-* `lhn()` gets the history number of the last command.
* `path()` manages the contents of `PATH` conveniently.
* `prompt()` sets up my interactive prompt.
* `pushd()` adds a default destination of `$HOME` to the `pushd` builtin.
diff --git a/bash/bashrc.d/lhn.bash b/bash/bashrc.d/lhn.bash
deleted file mode 100644
index 89c6f5da..00000000
--- a/bash/bashrc.d/lhn.bash
+++ /dev/null
@@ -1,7 +0,0 @@
-# Print the history number of the last command
-lhn () {
- local last
- last=$(fc -l -1) || return
- [[ -n $last ]] || return
- printf '%u\n' "${last%%[^0-9]*}"
-}
diff --git a/sh/shrc.d/lhn.sh b/sh/shrc.d/lhn.sh
new file mode 100644
index 00000000..15fced6e
--- /dev/null
+++ b/sh/shrc.d/lhn.sh
@@ -0,0 +1,12 @@
+# Print the history number of the last command
+# "fc" is specified by POSIX, but does not seem to be in dash, so its being
+# included here rather than in e.g. ~/.bashrc.d is a bit tenuous.
+lhn () {
+ if ! command -v fc >/dev/null 2>&1 ; then
+ printf 'lhn(): fc: command not found\n'
+ return 1
+ fi
+ set -- "$(fc -l -1)"
+ [ -n "$1" ] || return
+ printf '%u\n' "$1"
+}