TranslateProject/sources/tech/20190829 Variables in PowerShell.md
DarkSun 2535b4ffd1 选题: 20190829 Variables in PowerShell
sources/tech/20190829 Variables in PowerShell.md
2019-08-31 00:53:32 +08:00

7.9 KiB
Raw Blame History

Variables in PowerShell

In our miniseries Variables in Shells, learn how to handle local variables in PowerShell. Shells in a competition

In computer science (and casual computing), a variable is a location in memory that holds arbitrary information for later use. In other words, its a temporary storage container for you to put data into and get data out of. In the Bash shell, that data can be a word (a string, in computer lingo) or a number (an integer).

You may have never (knowingly) used a variable before on your computer, but you probably have used a variable in another area of your life. When you say things like "give me that" or "look at this," youre using grammatical variables (you think of them as pronouns). The meaning of "this" and "that" depends on whatever youre picturing in your mind, or whatever youre pointing to as an indicator for your audience to know what youre referring to. When you do math, you use variables to stand in for unknown values, even though you probably dont call them variables.

This article addresses variables in PowerShell, which runs on Windows, Linux, or Mac. Users of the open source Bash shell should refer to my article about variables in the Bash shell instead (although you can run PowerShell on Linux, and it is open source, so you can still follow along with this article).

Note: The examples in this article are from a PowerShell session running on the open source operating system Linux, so if youre on Windows or Mac the file paths will differ. However, Windows converts / to ** automatically, and all examples work across all platforms, provided that you substitute obvious differences (for instance, it is statistically unlikely that your username is seth).

What are variables for?

Whether you need variables in PowerShell depends on what you do in a terminal. For some users, variables are an essential means of managing data, while for others theyre minor and temporary conveniences, or for some, they may as well not exist.

Ultimately, variables are a tool. You can use them when you find a use for them, or leave them alone in the comfort of knowing theyre managed by your OS. Knowledge is power, though, and understanding how variables work in Bash can lead you to all kinds of unexpected creative problem-solving.

Set a variable

You dont need special permissions to create a variable. Theyre free to create, free to use, and generally harmless. In PowerShell, you create a variable by defining a variable name and then setting its value with the Set-Variable command. The example below creates a new variable called FOO and sets its value to the string $HOME/Documents:

`PS> Set-Variable -Name FOO -Value "$HOME/Documents"`

Success is eerily silent, so you may not feel confident that your variable got set. You can see the results for yourself with the Get-Variable (gv for short) command. To ensure that the variable is read exactly as you defined it, you can also wrap it in quotes. Doing so preserves any special characters that might appear in the variable; in this example, that doesnt apply, but its still a good habit to form:

PS> Get-Variable "FOO" -valueOnly
/home/seth/Documents

Notice that the contents of FOO arent exactly what you set. The literal string you set for the variable was $HOME/Documents, but now its showing up as /home/seth/Documents. This happened because you can nest variables. The $HOME variable points to the current users home directory, whether its in C:\Users on Windows, /home on Linux, or /Users on Mac. Since $HOME was embedded in FOO, that variable gets expanded when recalled. Using default variables in this way helps you write portable scripts that operate across platforms.

Variables usually are meant to convey information from one system to another. In this simple example, your variable is not very useful, but it can still communicate information. For instance, because the content of the FOO variable is a file path, you can use FOO as a shortcut to the directory its value references.

To reference the variable FOOs contents and not the variable itself, prepend the variable with a dollar sign ($):

PS> pwd
/home/seth
PS> cd "$FOO"
PS> pwd
/home/seth/Documents

Clear a variable

You can remove a variable with the Remove-Variable command:

PS> Remove-Variable -Name "FOO"
PS> gv "FOO"
gv : Cannot find a variable with the name 'FOO'.
[...]

In practice, removing a variable is not usually necessary. Variables are relatively "cheap," so you can create them and forget them when you dont need them anymore. However, there may be times you want to ensure a variable is empty to avoid conveying unwanted information to another process that might read that variable.

Create a new variable with collision protection

Sometimes, you may have reason to believe a variable was already set by you or some other process. If you would rather not override it, you can either use New-Variable, which is designed to fail if a variable with the same name already exists, or you can use a conditional statement to check for a variable first:

PS> New-Variable -Name FOO -Value "example"
New-Variable : A variable with name 'FOO' already exists.

Note: In these examples, assume that FOO is set to /home/seth/Documents.

Alternately, you can construct a simple if statement to check for an existing variable:

PS> if ( $FOO )
>> { gv FOO } else
>> { Set-Variable -Name "FOO" -Value "quux" }

Add to a variable

Instead of overwriting a variable, you can add to an existing one. In PowerShell, variables have diverse types, including string, integer, and array. When choosing to create a variable with, essentially, more than one value, you must decide whether you need a character-delimited string or an array. You may not care one way or the other, but the application receiving the variables data may expect one or the other, so make your choice based on your target.

To append data to a string variable, use the =+ syntax:

PS> gv FOO
foo
PS> $FOO =+ "$FOO,bar"
PS> gv FOO
foo,bar
PS> $FOO.getType().Name
String

Arrays are special types of variables in PowerShell and require an ArrayList object. Thats out of scope for this article, as it requires delving deeper into PowerShells .NET internals.

Go global with environment variables

So far the variables created in this article have been local, meaning that they apply only to the PowerShell session you create them in. To create variables that are accessible to other processes, you can create environment variables, which will be covered in a future article.


via: https://opensource.com/article/19/8/variables-powershell

作者:Seth Kenlon 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出