zsh: imprime texto literal a partir da linha de comando

3

Eu quero imprimir algum texto literal, incluindo coisas como ; e # .

Em bash , posso usar (crédito) :

echo_literally_helper() {
  local str; str=$(history 1)
  str=${str#[\ ]*[0-9]*\ *echo-literally\ }  # remove leading space number space
  echo "$str"
}
alias echo-literally='echo_literally_helper #'

Então:

$ echo-literally a; b; c
a; b; c

Como eu faria o mesmo em zsh ?

    
por Tom Hale 16.07.2018 / 10:05

1 resposta

1

Seria possível usar $history e < a href="http://zsh.sourceforge.net/Doc/Release/Options.html#index-INTERACTIVECOMMENTS"> INTERACTIVE_COMMENTS :

echo_literally_helper () {
  local str="$history[$(print -P %h)]"
  echo "${str#*echo_literally\ }"
}
alias echo_literally='echo_literally_helper #'

# for using '$history'. (does not needed in zsh-5.5.1 here, though.)
zmodload zsh/parameter

# zsh does not enable this option by default, so turn on.
setopt interactivecomments

Então:

% echo_literally a; b; c
a; b; c

Nota: ele usa print -P %h e $history para obter o comando do histórico atual em vez do comando history (ou fc ) bulitin.

Aqui estão algumas referências para documentações zsh.

history
This associative array maps history event numbers to the full history lines.
...

-- zshmodules(1): zsh/history, zsh modules

-

INTERACTIVE_COMMENTS (-k)
Allow comments even in interactive shells.

-- zshoptions(1): Input/Output, Options

-

Comments:
In non-interactive shells, or in interactive shells with the INTERACTIVE_COMMENTS option set, a word beginning with the third character of the histchars parameter (‘#’ by default) causes that word and all the following characters up to a newline to be ignored.

-- zshmisc(1): Comments, Shell Grammar

-

%h
%! Current history event number.

-- zshmisc(1): Shell state, Simple Prompt Escapes, Prompt Expansion

    
por 18.07.2018 / 05:45