LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell command (http://www.linuxquestions.org/questions/linux-newbie-8/shell-command-462873/)

prospekrisal 07-11-2006 02:37 AM

shell command
 
hi guys, would you tell me what's wrong with these commands!!
1) fahrisal@linux:~> name=1
fahrisal@linux:~> $name
bash: 1: command not found

2)fahrisal@linux:~> name=Risal
fahrisal@linux:~> $name
bash: Risal: command not found

3)fahrisal@linux:~> name=Risal kadrie
bash: kadrie: command not found

What is an active process in the system? how to disply it?

cdhgee 07-11-2006 02:55 AM

To start with, you are trying to execute these variables rather than display them. What I think you want would be:

Code:

fahrisal@linux:~> name=1
fahrisal@linux:~> echo $name

There is an additional problem in number 3: Linux uses whitespace as delimiters between command-line options, so if you want to assign a string containing a space, you need to quote the string thus:

Code:

fahrisal@linux:~> name="Risal kadrie"
Then you should get the desired effect:

Code:

david@malvern:~ $ name=1

david@malvern:~ $ echo $name
1

david@malvern:~ $ name=Risal

david@malvern:~ $ echo $name
Risal

david@malvern:~ $ name="Risal kadrie"

david@malvern:~ $ echo $name
Risal kadrie


cs-cam 07-11-2006 02:55 AM

Code:

[ sylvester :: cam ]-> name=1
[ sylvester :: cam ]-> echo $name
1
[ sylvester :: cam ]-> name="Cameron Daniel"
[ sylvester :: cam ]-> echo $name
Cameron Daniel


prospekrisal 07-11-2006 02:56 AM

echo $name

name="Risal kadrie"

cs-cam 07-11-2006 02:56 AM

Damn, beaten to the punch :p

prospekrisal 07-11-2006 02:58 AM

What is an active process in the system? how to disply it?

b0uncer 07-11-2006 02:59 AM

Sure. First you did right,

name=1

should tell name equals to 1. After this you should use export like

Code:

export name
and after this you could see the variable, not by typing $name (which tells bash it's a program called $name) but using echo instead:

Code:

echo $name
The export is not necessary (at least in all situations) but might help.

EDIT: and I swear I just saw only three persons active on this thread..where did those other come from? Huh? And why haven't they *still* read my suggestion about an answer-lock of some kind to prevent a waste of posts like this..

cdhgee 07-11-2006 03:01 AM

Quote:

Originally Posted by prospekrisal
What is an active process in the system? how to disply it?

An active process is just a program that is running. To display a list of all processes running, you can use this command:

Code:

ps -ef
You can then filter that down using grep to find exactly what you're looking for.

cdhgee 07-11-2006 03:04 AM

Quote:

Originally Posted by b0uncer
The export is not necessary (at least in all situations) but might help.

Correct - export is only necessary if you wish other programs to be able to access your variable. For example, if you are setting an environment variable that another program runs. If you don't export the variable, its scope is treated as being local to the shell/script (depending on where it's being set) - i.e. not part of the environment.

b0uncer 07-11-2006 03:04 AM

...and even use
Code:

ps aux
to get some more (possibly unneeded) information. Though that "aux" combination doesn't seem to work on every ps command I've encountered..

EDIT:
Quote:

Correct - export is only necessary if you wish other programs to be able to access your variable. For example, if you are setting an environment variable that another program runs. If you don't export the variable, its scope is treated as being local to the shell/script (depending on where it's being set) - i.e. not part of the environment.
Thanks - that's by far the best (compact) explanation for "export" I've heard.

jschiwal 07-11-2006 03:24 AM

I think the "export" command needs more explanation. When you start a program, it inherits the environment with variables defined such as $HOME. Your program, let's call it "parent", might define one or more variables. Defining a variable places it in local memory and not the environment. If you call another program in the "parent" program, let's call it child, the variables you set will not be defined in "child" unless you export them.

If you define a variable in child, even if you export it, it won't be seen in the parent after child finishes. This is because the child program runs in a subshell. What you could do is source the child program either like:
source child
or
. child

Now the child program is read in and executed in the same shell instead of a subshell, and the variable is defined.

Code:

cat parent
#!/bin/bash
echo $HOME

name=smith
echo $name
./child
echo $name

cat child
#!/bin/bash
echo $HOME
echo $name

./parent
/home/jsmith
smith
/home/jsmith

smith

The variable "name" wasn't defined in child, because "export name" wasn't run before calling child.


All times are GMT -5. The time now is 08:16 PM.