2

I am trying to format a variable in linux

str="Initial Value = 168" 
echo "New Value=$(echo $str|  cut -d '=' -f2);">>test.txt

I am expecting the following output

Value = 168;

But instead get

Value = 168 ^M;
5
  • What's your input? please separate the input and command. Commented Jun 26, 2014 at 19:03
  • 1
    For mor complex formatting, try printf shell command. (And that ^M is CR, so you have some odd line break there.) Commented Jun 26, 2014 at 19:04
  • My input string value is "Initial Value = 168 " Commented Jun 26, 2014 at 19:04
  • Could it be that your input string had a CR+LF instead of LF only? If your snippet is in a file, your editor may be using wrong line breaks. In that case the string "...168<CR><LF>" is interpreted as a text which ends with <CR> in the end. Commented Jun 26, 2014 at 19:05
  • I am trying format before echo to the file Commented Jun 26, 2014 at 19:08

4 Answers 4

2

Don't edit your bash script on DOS or Windows. You can run dos2unix on the bash script. The issue is that Windows uses "\r\n" as a line separator, Linux uses "\n". You can also manually remove the "\r" characters in an editor on Linux.

Sign up to request clarification or add additional context in comments.

Comments

2
str="Initial Value = 168"
newstr="${str##* }"
echo "$newstr"  # 168

pattern matching is the way to go.

Comments

1

Try this:

#! /bin/bash

str="Initial Value = 168"

awk '{print $2"="$4}' <<< $str > test.txt

Output:

cat test.txt
Value=168

I've got comment saying that it doesn't address ^M, I actually does:

echo -e 'Initial Value = 168 \r' | cat -A 
Initial Value = 168 ^M$

After awk:

echo -e 'Initial Value = 168 \r' | awk '{print $2"="$4}' | cat -A
Value=168$

Comments

0

First off, always quote your variables.

#!/bin/bash

str="Initial Value = 168"
echo "New Value=$(echo "$str" | cut -d '=' -f2);"

For me, this results in the output:

New Value= 168;

If you're getting a carriage return between the digits and the semicolon, then something may be wrong with your echo, or perhaps your input data is not what you think it is. Perhaps you're editing your script on a Windows machine and copying it back, and your variable assignment is getting DOS-style newlines. From the information you've provided in your question, I can't tell.

At any rate I wouldn't do things this way. I'd use printf.

#!/bin/bash

str="Initial Value = 168"
value=${str##*=}
printf "New Value=%d;\n" "$value"

The output of printf is predictable, and it handily strips off gunk like whitespace when you don't want it.

Note the replacement of your cut. The functionality of bash built-ins is documented in the Bash man page under "Parameter Expansion", if you want to look it up. The replacement I've included here is not precisely the same functionality as what you've got in your question, but is functionally equivalent for the sample data you've provided.

2 Comments

Recommending echo | cut rather than bash builtins (such as parameter expansion or read) is somewhat unfortunate.
@CharlesDuffy - fixed. Wanted to get the answer in, then clean it up. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.