Skip to main content
deleted 2 characters in body
Source Link
Gilles 'SO- stop being evil'
  • 865.5k
  • 205
  • 1.8k
  • 2.3k

Use redirection, command substitution, and parameter expansion. Double quotes are needed to preserve whitespace and special characters. The trailing x saves the trailing whitespacenewlines which would be otherwise removed in the command substitution.

#!/bin/bash
echo "$var"x > file
unset var
var="$(< file)"
var=${var%x}

Use redirection, command substitution, and parameter expansion. Double quotes are needed to preserve whitespace and special characters. The trailing x saves the trailing whitespace which would be otherwise removed in the command substitution.

#!/bin/bash
echo "$var"x > file
unset var
var="$(< file)"
var=${var%x}

Use redirection, command substitution, and parameter expansion. Double quotes are needed to preserve whitespace and special characters. The trailing x saves the trailing newlines which would be otherwise removed in the command substitution.

#!/bin/bash
echo "$var"x > file
unset var
var="$(< file)"
var=${var%x}
Source Link
choroba
  • 49.4k
  • 7
  • 92
  • 118

Use redirection, command substitution, and parameter expansion. Double quotes are needed to preserve whitespace and special characters. The trailing x saves the trailing whitespace which would be otherwise removed in the command substitution.

#!/bin/bash
echo "$var"x > file
unset var
var="$(< file)"
var=${var%x}