Solaris / OpenSolarisThis forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
check the grep man/texinfo page - on your computer or online.
(man grep or info grep)
ps -eaf produces screeds of output (try it) to stdout. What grep does is search through this output for any line which include a stated string - and displaying that. In this case, only lines containing the letters "mnon" will be displayed.
The use you have shown is the most common way of using grep - as an output filter.
Another example:
ls -l will list the long version of all files in a directory. Some directories contain 100s of files. If you just want to see the jpeg images (if any) you would do: "ls -l | grep jpg". And you can make a list of them with "ls -l | grep jpj > jpglist.txt"
But grep is used whenever you want to sort text. Like if you want to know how many times the word "the" appears in Moby Dick... or if you have accidentally deleted a file.
In general - if you want to know what a command does in a broader sence, google "man <command name>" and you'll get the technical description.
I use grep mostly to find out which file(s) contain some certain thing. Like which source file contains a particular variable or function. Really, the uses are so diverse it would be impossible to make any sort of comprehensive list. I will say, though, that if you get yourself acquainted with the Unix standard notation for regular expressions, you will probably understand a lot better how grep was intended to be used.
As for 'env', I never even heard of it until I read your post. I usually use either 'set' or 'export' to do what 'env' seems to do. I doubt many people use this much.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
Originally Posted by theNbomr
As for 'env', I never even heard of it until I read your post. I usually use either 'set' or 'export' to do what 'env' seems to do. I doubt many people use this much.
--- rod.
env is portable way to set and export a variable for a single command. It is simpler than the set / export builtins, and doesn't need a shell to be processed.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
Originally Posted by theNbomr
You are correct, but you could also have said
Code:
(export a=b; command)
which isn't really a lot more wordy than the equivalent 'env' method. I know I'm picking gnits.
--- rod.
You are right, and this command can even be made shorter with:
Code:
a=b command
The env point is that it needs not a specific shell to be understood. For example, not your example nor my last up here are correct with the bourne shell (the real one) or with csh.
In fact env doesn't even need a shell so you can call it directly from a C program with one of the exec family calls.
As a real life example, I'm using it in custom icewm menu entries to set the locale or the ld library path to specific values.
If you define the environment variable in a shell, it affects all commands/executables you run, until you unset the env variable.
On the other hand, using "env a=b command", affects only "command", but nothing else.
Actually, in bash, which seems to be the shell used by most linux users, environment variables are not passed on to child processes, unless they are exported. To use your example, try
Code:
(LD_DEBUG=files; date)
vs.
(export LD_DEBUG=files; date)
Again, I know I'm picking nits, here, and this is more of a 'bash-ism' than an 'env' thing.
The point about 'env' being independent of any particular shell is a good one.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
Originally Posted by theNbomr
Actually, in bash, which seems to be the shell used by most linux users,
But this is a non Linux Forum, Solaris precisely, where the shell of choice is more ksh, although bash is a good contender, especially amongst beginners.
Quote:
environment variables are not passed on to child processes, unless they are exported.
This is true for all shells, although you can set an option to have variables exported by default.
Quote:
To use your example, try
Code:
(LD_DEBUG=files; date)
vs.
(export LD_DEBUG=files; date)
You seem to miss that the latter is equivalent to
Code:
(LD_DEBUG=files date)
which itself is launching a subshell for no reason, so can be itself simplified to
Code:
LD_DEBUG=files date
Making the "env" command useless when running a modern shell (ksh, bash, ...)
Quote:
Again, I know I'm picking nits, here, and this is more of a 'bash-ism' than an 'env' thing.
It is not a bash-ism, all bourne shells descendant and clones are behaving that way.
Quote:
The point about 'env' being independent of any particular shell is a good one.
--- rod.
env is not only independant of a shell, but in fact can live without a shell, something other variable assignement techniques rely on.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.