How to print $ in shell script?
You can use:
msg1='$'
ms="${msg1}msg1"
msg2="$ms two"
msg3="$msg2 three"
echo "$msg3"
OUTPUT:
$msg1 two three
PS: Take note of ${msg1}
syntax to create variable boundary around msg1
. This is used to avoid it making it $msg1msg1
Shell script printing contents of variable containing output of a command removes newline characters
If you want to preserve the newlines, enclose the variable in double quotes:
echo "$stuff"
When you write it without the double quotes, the shell expands $stuff
into a space-separated list of words (where 'words' are sequences of non-space characters, and the space characters are blanks and tabs and newlines; upon experimentation, it seems that form feeds, carriage returns and back-spaces are not counted as space).
Demonstrating interpretation of control characters as white space. ASCII 8 is backspace, 9 is tab, 10 is new line (LF), 11 is vertical tab, 12 is form feed, 13 is carriage return. The first command generates a sequence of characters separated by the various control characters. The second command echoes with the result with the original characters preserved - see the hex dump. The third command echoes the result with the shell splitting the words; you can see that the tab and newline were replaced by blank (0x20).
$ x=$(./ascii 64 65 8 66 67 9 68 69 10 70 71 11 72 73 12 74 75 13 76 77)
$ echo "$x" | odx
0x0000: 40 41 08 42 43 09 44 45 0A 46 47 0B 48 49 0C 4A @A.BC.DE.FG.HI.J
0x0010: 4B 0D 4C 4D 0A K.LM.
0x0015:
$ echo $x | odx
0x0000: 40 41 08 42 43 20 44 45 20 46 47 0B 48 49 0C 4A @A.BC DE FG.HI.J
0x0010: 4B 0D 4C 4D 0A K.LM.
0x0015:
$
Print to screen after setting log file in shell script
Could you please try following. To remove effect of your exec &> file
command you can use exec &>/dev/tty
to bring writing back to standard output and standard errors. Then we could use tee -a
command to write it on screen as well as on file.
cat my_script.sh
log_file="test_file"
exec &> $log_file
echo "12131313113...."
echo "test bla bla bla.."
exec &>/dev/tty
echo "test again...." | tee -a "$log_file"
Now when we run the script the last line should be printed on screen and should be saved into output file too.
./test.sh
test again....
##See the output of test file..
cat test_file
12131313113....
test bla bla bla..
test again....
Or alternatively:echo "output" > /dev/tty
Print periodic progress messages on the same line
command|awk '{ printf "Progress: %s%% \r", $1}'
This one is with ProgressBar
command|awk '{printf "\rProgress: " $1 "%% ["; for(c=0;c<$1;c++) printf "#"; printf "]" }'
Progress: 60% [############################################################]
How to print something to the right-most of the console in Linux shell script
Using bash
and printf
:
printf "%-$(( COLUMNS - ${#errorNumber} ))s%s" \
"$filePath" "$errorNumber"
How it works:
$COLUMNS
is the shell's terminal width.printf
does left alignment by putting a-
after the%
. Soprintf "%-25s%s\n" foo bar
prints "foo", then 22 spaces, then "bar".bash
uses the#
as a parameter length variable prefix, so ifx=foo
, then${#x}
is 3.
Fancy version, suppose the two variables are longer than will fit in one column; if so print them on as many lines as are needed:
printf "%-$(( COLUMNS * ( 1 + ( ${#filePath} + ${#errorNumber} ) / COLUMNS ) \
- ${#errorNumber} ))s%s" "$filePath" "$errorNumber"
Generalized to a function. Syntax is printfLR foo bar
, or printfLR < file
:
printfLR() { if [ "$1" ] ; then echo "$@" ; else cat ; fi |
while read l r ; do
printf "%-$(( ( 1 + ( ${#l} + ${#r} ) / COLUMNS ) \
* COLUMNS - ${#r} ))s%s" "$l" "$r"
done ; }
Test with:
# command line args
printfLR foo bar
# stdin
fortune | tr -s ' \t' '\n\n' | paste - - | printfLR
Related Topics
Linux: Update Embedded Resource from Executable
Does Grub Switch to Protected Mode
How to Test My Bash Script on Older Versions of Bash
How to Use "Xargs" Properly When Argument List Is Too Long
How to Find Files Containing a String Using Egrep
How to Capture Remote System Network Traffic
Bash Script to Rename All Files in a Directory
User-Space Memory Editing Programs
Linux: Processes and Threads in a Multi-Core Cpu
Linux: Find a List of Files in a Dictionary Recursively
Shell Script to Check Ubuntu Version and Then Copy Files
Find Files and Print Only Their Parent Directories
How to Stop Sed from Buffering
Bash: Getting Percentage from a Frequency Table