In Linux, you can try this:
top -bn1 > output.txt
From man top
:
-b : Batch-mode operation
Starts top in 'Batch' mode, which could be useful for sending
output from top to other programs or to a file. In this
mode, top will not accept input and runs until the iterations
limit you've set with the '-n' command-line option or until
killed.
....
-n : Number-of-iterations limit as: -n number
Specifies the maximum number of iterations, or frames, top
should produce before ending.
With OS X, try:
top -l 1
From top OSX manpage:
-l <samples>
Use logging mode and display <samples> samples, even if
standard output is a terminal. 0 is treated as infinity.
Rather than redisplaying, output is periodically printed in
raw form. Note that the first sample displayed will have an
invalid %CPU displayed for each process, as it is calculated
using the delta between samples.
If it is an sh
script - as in, it explicitly references #!/bin/sh
- which might still be bash
but would be like invoking it with --posix --no-rc --no-profile
- then you can specify the ENV
file with the ENV
environment variable:
ENV=/path/to/rcfile sh
Specific variables need either to be declared on the command-line - as above for $ENV
- or else with export
. For example, for $PATH
you do:
export "PATH=$PATH:/some/more/paths"; sh
The $BASH_ENV
variable you reference is not a file you need to source - and it isn't interpreted anyway when bash
is invoked as sh
- but is rather a path to a file that is sourced when a non-interactive bash
shell is invoked - such as with a script that specifies the:
#!/bin/bash
...or whatever bang line.
Another way you might like to invoke your script/shell is with the env
utility. It can be used to explicitly remove values from the environment, or else, as is usually easiest, to wipe it clean from the start:
env - BASH_ENV=/path/to/rcfile /usr/bin/bash /some/script/file
That will tell env
to invoke the /usr/bin/bash
command - with all its arguments appended - with the $BASH_ENV
environment variable specified, but otherwise with a clean environment entirely.
Best Answer
top -b -n 1
From
man top
: