null
null

Reputation: 85

Why does backtick placement matter?

I'm trying to better understand how backticks work in PowerShell. This works and executes the ipconfig command:

$a = "ipc"
$b = "onf`ig"


iex $a$b

However, if the backtick is moved one character to the left, before the "f", the command breaks...

$a = "ipc"
$b = "on`fig"


iex $a$b

Another example of this:

who`ami

If the backtick is anywhere else, the whoami command will work just fine. With a backtick in the middle, it breaks. What's happening here? Why does the placement of the backtick's matter so much?

Upvotes: 1

Views: 135

Answers (2)

postanote
postanote

Reputation: 16096

Though I don't agree with all the author of this article says. Most of the is valid when it comes to use of the graveyard accents\bact tick.

It does have its use cases, but not for what you are showing.

Bye Bye Backtick: Natural Line Continuations in PowerShell]

See also:

about_Special_Characters - PowerShell | Microsoft Docs

PowerShell - Special Characters And Tokens

Grave_accent

Use in programming Programmers use the grave accent symbol as a separate character (i.e., not combined with any letter) for a number of tasks. In this role, it is known as a backquote, or backtick.

Many of the Unix shells and the programming languages Perl, PHP, and Ruby use pairs of this character to indicate command substitution, that is, substitution of the standard output from one command into a line of text defining another command. For example, using $ as the symbol representing a terminal prompt, the code line...

How-to: Escape characters, Delimiters and Quotes

Upvotes: 0

wasif
wasif

Reputation: 15488

These are becuase some special characters in powershell.

In powershell there are some special characters which are not in standard character set. They start with back tick to show special meaning. They are:

`0 Null

`a Alert

`b Backspace

`e Escape

`f Form feed

`n New line

`r Carriage return

`t Horizontal tab

`u{x} Unicode escape sequence

`v Vertical tab

Here when you escape "a" with backtick means alert powershell (whoami) and when you escape "f" with backtick means form feed (ipconfig), so both commands break.

And when you escape the other character, commands don't break becuase then characters not render the special meaning.

Upvotes: 3

Related Questions