4

I need to execute the dos2unix command in a variable. With files we just do dos2unix myfile.txt. How can I do that with a variable? For example:

variable="bla\r"
dos2unix $variable

Sugestions using other commands are also welcome.

PS.: I cannot perform a dos2unix on the file from where I'm reading the text.

hbelmiro
  • 987
  • 11
  • 31
  • 2
    BTW, `variable="bla\r"` is putting two characters, a backslash and an `r`, in the variable, not a single-character carriage return. If your shell is bash, one correct assignment for doing this would be `variable=bla$'\r'`. – Charles Duffy Nov 03 '15 at 18:41
  • ...you can use `'\r'` or `"\r"` for `tr`, `printf` or (GNU's non-POSIX-compliant) `echo -e` because it's doing backslash interpretation itself, rather than expecting to use the character as literal input. – Charles Duffy Nov 03 '15 at 18:42

3 Answers3

3

dos2unix can read from standard input, so you can write echo "$variable" | dos2unix. Try this:

$ variable=$'bla\r'

$ echo "$variable" | cat -A
bla^M$

$ echo "$variable" | dos2unix | cat -A
bla$
user000001
  • 32,226
  • 12
  • 81
  • 108
  • Arguably, if we're following the POSIX echo specification, the output of `echo "$variable"` is poorly defined by the standard -- any backslash-escape sequences inside that content will make the output undefined (whereas an echo with XSI extensions will expand them by default). See http://pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html if you're looking for a fun time, and consider `printf '%s\n' "$variable"` instead when passing content unmodified (other than addition of a trailing newline, or just `'%s'` should that be undesired) is important. – Charles Duffy Nov 03 '15 at 19:10
  • @CharlesDuffy But hasn't the backslash-escape sequence already been converted to the corresponding control character, when we write `$'blah\r'`? echo shouldn't see any backslashes. – user000001 Nov 03 '15 at 19:18
  • Sure -- I'm not talking about this specific test data; what we're building here should ideally be reusable with arbitrary content, which *could* potentially include content with backslash literals. – Charles Duffy Nov 03 '15 at 19:30
3

There's no need for any external command here; you can use parameter expansion to remove CRs using only functionality built into the shell itself, thus both faster to execute (with reasonably short strings) and guaranteed to work on any system that has bash (or a similarly extended shell, such as ksh93 or zsh), even without dos2unix installed:

$ PS1='> ' # for readability, to distinguish output starting with '$' literals
> variable_in=$'bla\r'
> variable_out=${variable_in//$'\r'/}
> printf '%q\n' "$variable_in"
$'bla\r'
> printf '%q\n' "$variable_out"
bla
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
2

You can use use tr:

echo "$variable"| tr -d '\r'

or

tr -d '\r' <<< "$variable"
anubhava
  • 761,203
  • 64
  • 569
  • 643