You can access the just-executed command line with the history
built-in. (I have no idea why history 1
prints the just-executed command line but fc -nl -1
prints the previous commmand, as does fc -nl 0
.)
PROMPT_COMMAND='echo -en "\033[38;5;2m"; history 1; echo -en "\033[0m\n"'
This prints a number before the command text. Here's a version that removes the number. (It may be incorrect if you go beyond 99999 history lines, I don't know how bash formats the number then.)
prompt_function () {
local prompt_history="$(history 1)"
prompt_history=${prompt_history:7}
echo -En $'\033[38;5;2m'"$prompt_history"$'\033[0m\n'
}
PROMPT_COMMAND=prompt_function
(Note that echo -en ..."$prompt_history"...
would expand backslashes in the command line, so I use echo -E
and let the shell expansion generate the control characters with $''
.).
if ($mystr:q =~ *'\
'*) echo yes
should work in some implementations and versions of csh
(like the csh
and tcsh
ones found on Debian). In some others (like the one found on Solaris 10), you may have better luck with
set nl = '\
'
if ($mystr:q =~ *$nl:q*) echo yes
Most people have given up trying to write reliable scripts with csh
by now. Why would you use csh
in this century?
This code works for me (outputs no
) in tcsh 6.17.00 (Astron) 2009-07-10 (x86_64-unknown-linux) options wide,nls,dl,al,kan,rh,color,filec
set mystr = '1234ABC\
-------\
FOOBAR'
if ($mystr:q !~ *'\
'*) then
echo yes
else
echo no
endif
Note that if you do:
set var = `some command`
csh
stores each word (blank separated) of the output of some command
in several elements of the var
array.
With:
set var = "`some command`"
it stores each non-empty line in elements of the array.
It looks like one cannot1 store the output of a command whole into a variable in (t)csh
, so your only option would be:
set var = "`some command`" # note that it removes the empty lines
if ($#var == 1)...
1 Strictly speaking, that's not true, one could do something like:
set x = "`some command | paste -d. /dev/null -`"
set var = ""
set nl = '\
'
foreach i ($x:q)
set i = $i:s/.//:q
set var = $var:q$i:q$nl:q
end
(of course, it may not work in all csh
implementations/versions)
Best Answer
This works in
bash
. I do not know how compatible this is.