history
is a Bash builtin, i.e. an internal command that can only be invoked from inside a Bash session; thus, by definition you cannot invoke it directly from PowerShell.
In PowerShell history
is an alias of PowerShell's own Get-History
cmdlet, where -c
references the -Count
parameter, which requires an argument (the number of history entries to retrieve).
- Unfortunately,
Clear-History
is not enough to clear PowerShell's session history as of PowerShell 7.2, because it only clear's one history (PowerShell's own), not also the one provided by the PSReadLine
module used for command-line editing by default - see this answer.
Your attempt to call bash
explicitly with your command - bash history -c
- is syntactically flawed (see bottom section).
However, even fixing the syntax problem - bash -c 'history -c'
- does not clear Bash's history - it seemingly has no effect (and adding the -i
option doesn't help) - I don't know why.
The workaround is to remove the file that underlies Bash's (persisted) command history directly:
if (Test-Path $HOME\.bash_history) { Remove-Item -Force $HOME\.bash_history }
To answer the general question implied by the post's title:
To pass a command with arguments to bash
for execution, pass it to bash -c
, as a single string; e.g.:
bash -c 'date +%s'
Without -c
, the first argument would be interpreted as the name or path of a script file.
Note that any additional arguments following the first -c
argument would become the arguments to the first argument; that is, the first argument acts as a mini-script that can receive arguments the way scripts usually do, via $1
, ...:
# Note: the second argument, "-", becomes $0 in Bash terms,
# i.e. the name of the script
PS> bash -c 'echo $0; echo arg count: $#' self one two
self
arg count: 2