简体   繁体   English

PowerShell 相当于 bash 中的“dirname $0”

[英]PowerShell equivalent of “dirname $0” from bash

I've used dirname "$0" which is an idiom to determine the path of the running script in bash, eg:我使用dirname "$0"来确定 bash 中运行脚本的路径,例如:

pushd "$(dirname "$0")"
data_dir="$(dirname "$0")/data/"

What's PowerShell equivalent of the above idiom?什么是上述成语的 PowerShell 等价物?

Since PowerShell version 3.0, the execution context provides 2 script-scoped automatic variables:从 PowerShell 3.0 版开始,执行上下文提供了 2 个脚本范围的自动变量:

  • $PSCommandPath - the file system path to the executing script, eg. $PSCommandPath - 执行脚本的文件系统路径,例如。 C:\path\to\script.ps1
  • $PSScriptRoot - the immediate parent folder of the script, eg. $PSScriptRoot - 脚本的直接父文件夹,例如。 C:\path\to

So the equivalent of your last statement would be as follows in PowerShell:因此,您的最后一条语句在 PowerShell 中将如下所示:

$dataDir = Join-Path $PSScriptRoot data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM