0

When I use echo $’hi\nhi’ > /etc/list.list in an executable script text file I create comprising multiple commands on Kali Linux 2020.4 Live USB it:

  1. Includes the $ in the files text while disappearing the 2 ’’ symbols.

  2. Does not work to create a new line, separating the 2 hi’s from each other, instead just showing the \n with no separation at all.

but when I use the same command outside of the executable or in the plain terminal it works.

Did Kali 2020.4 change from 2020.3 to not allow the echo formatting I use within an executable text file, because it use to work flawlessly? Is there some other command I can use for echo that will create a new line in a 1 line command?

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • 1
    What's the first line of the script (specifically is it a #! line identifying the interpreter to use for the file), and how do you run the script? – Chris Davies Feb 22 '21 at 17:36

1 Answers1

1

There are a number of problems with your command, fortunately they're all easy to resolve:

  1. As Quasimodo mentioned in their comment, is not a valid character for delimiting strings within Bourne Again Shell (aka "BASH", the default shell interpreter on Kali Linux). Replacing that with single quotes (') or double quotes (") will be required. Here is some great information on quoting within BASH.

  2. You are attempting to use "escape sequences" within your echo command, but are not providing the -e flag.

  3. Based on the fact that you don't want the dollar sign character ($) in your output, it would appear you are attempting to perform "parameter expansion" or (as noted by steeldriver in the comments) to create an [ANSI Quoted String] (https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html).

As per the GNU BASH documentation for ANSI Quoting:

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded

An understanding of escape sequences is the important distinction in this answer. One can choose where shell expansion is occurring: at the stage of running echo or in the use of the ANSI quoting. This distinction is made because there are situations where specifying these sorts of expansions must be done outside of echo to have the desired effect. After thinking about this further, I recalled awk is a tool where I have seen this issue come up:

Use backslash or single-quotes for field separation

In general, I would also review the "Newbie traps - Variables" section of the BASH Hackers wiki, specifically the subsections on "Setting Variables" and "Expanding (using) variables".

These are basic syntax issues with BASH itself and would not be affected by minor version updates to Kali.

EDIT: Added the breakdown of 3a/3b to include information from steeldriver's comment.