Preserve trailing whitespace in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Preserve trailing whitespace in variable
# 1  
Old 11-02-2012
Preserve trailing whitespace in variable

Hello,

I wondering how I can echo a string without having the trailing whitespace removed.

For example I have a string str="TESTING123 " that I need to hash using sha1. I get the correct answer when I run the line below from the terminal

Code:
$ echo -n "TESTING123 " | openssl sha1
fe1a126175cf589be634fc3a41a7701a78f5ad28

However when I pass the string as a variable I get a different answer as the trailing whitespace is removed.

Code:
str="TESTING123 "
echo -n $str | openssl sha1
18aa7ac11356238ef89cfbd2875727dcd054d6ed

Is there a particular way I should declare this variable or something?

Regards,
Colin
# 2  
Old 11-02-2012
Code:
echo -n "$str"

# 3  
Old 11-03-2012
Simple as...

Thanks for your help balajesuri
# 4  
Old 11-05-2012
It's advised to always double quote variable in a shell script.
Code:
"$var"

The reason for that is treating shell code and shell data in variables equivalently! Not like in other languages that line of code is different entity/type of text than line of string stored under variable. Here it is the same.

In your case $str is expanded correctly with space at the end, but it is then just space in a command line, so it is discarded

You can find more info in this article http://www.cofoh.com/white-shell
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to preserve the value of a variable from being overwritten?

Hi All, I am new new to unix.com, I have a question related to shell scripting. We have a Oracle database backup shell script, which can be used for taking full, incremental & archive log backup based on the parameters passed. Within the script we export a variable as export... (5 Replies)
Discussion started by: veeresh_15
5 Replies

2. Shell Programming and Scripting

awk - How to preserve whitespace?

Given a file: # configuration file for newsyslog # $FreeBSD: /repoman/r/ncvs/src/etc/newsyslog.conf,v 1.50 2005/03/02 00:40:55 brooks Exp $ # # Entries which do not specify the '/pid_file' field will cause the # syslogd process to be signalled when that log file is rotated. This # action... (10 Replies)
Discussion started by: jnojr
10 Replies

3. UNIX for Dummies Questions & Answers

Want to add trailing spaces to the variable

I want to keep string/varible length to 10 even its actual length is less than 10(may be no value). so, i want to add trailing spaces to my string. :wall: "typeset -L10 myvarible" is not working, its saying invalid typset -L option. Can you please advise. (4 Replies)
Discussion started by: djaks111
4 Replies

4. UNIX for Dummies Questions & Answers

delete trailing whitespace from end of each line in column 1 only

Hi All. How can I convert this: ABC_1_1 ABC_1_2 ABC_1_3 into this: ABC_1 1 ABC_1 2 ABC_1 3 I tried this command but it is not working: awk '{sub(/+$/,"\t", $1)}{print}' Any suggestions on how to fix this? Thank you :wall: Please use code tags when posting data and... (3 Replies)
Discussion started by: danieladna
3 Replies

5. Shell Programming and Scripting

Date Format - preserve whitespace

I am trying to change the date format for the following: YESTER=`TZ=aaa24 date +%b" "%d | sed 's/0/ /'` TraceList=$(ls -ltR /pdt/logs | grep "$YESTER" | awk '{print $9}') CMD2=$(find /disk/dan/dansFiles/pass/logs/$TList -name cmd\* -prune) what I am trying to do in the above... (1 Reply)
Discussion started by: ther2000
1 Replies

6. Shell Programming and Scripting

Preserve space in variable of AWK

This seems to be a stupid basic question, but I cant get the space to stick in the awk variable. I do use this command to grep a time range of the log file. cat /var/log/daemon.log | awk '$0>=from&&$0<=to' from="$(date +%b" "%e" "%H:%M:%S -d -24hour)" to="$(date +%b" "%e" "%H:%M:%S)" I now... (9 Replies)
Discussion started by: Jotne
9 Replies

7. Shell Programming and Scripting

How to remove trailing spaces from a variable?

I am getting a value from a csv file using CUT command, however the command extracting the records with trailing spaces. I am using the result into a sql session to fetch data, because of the trailing spaces the sql session is unable to fetch any data. Please let me know, how to remove this... (2 Replies)
Discussion started by: mady135
2 Replies

8. Shell Programming and Scripting

Unable to assign value to variable using awk coz of whitespace in value

Unix gurus, I have a file as below, which is basically the result set obtained from a sql query on an Oracle database. ID PROG_NAME USER_PROG_NAME -------- --------------- ---------------------------------------- 33045 INCOIN Import Items 42690 ... (3 Replies)
Discussion started by: sunpraveen
3 Replies

9. Shell Programming and Scripting

trimming trailing slashes in variable

I'm using this thread as an example, but can't seem to apply it to my situation. I'm trying to strip the trailing slash (/) from an input argument. Here's a snippet of my command line input and the troublesome code: $ script_name -s "../pathname/dir/" snip 8< ... while getopts :s:... (5 Replies)
Discussion started by: ricksj
5 Replies

10. Shell Programming and Scripting

How to delete trailing zeros from a variable

Hi All I want to delete trailing zeros from varible. ex: if variable value is 1234.567000 result as 1234.567 if variable has 1234.0000 result as 1234 if variable as abcd.fgh result as abcd.fgh Can somone give me a solution using awk? (16 Replies)
Discussion started by: Chandu2u
16 Replies
Login or Register to Ask a Question