What does `setenv DISPLAY name:1001.0` mean in Linux? - linux

I'm doing some work using Linux server, after I log on to the server,the tutorial says:
If you need to run any program which will open a window, like xterm, from these servers, you need to set display first.
To do that, I need to execute setenv DISPLAY name:portnum.What does this command really do?If I don't execute this command,what will happen?And what is xterm?

setenv is specific and peculiar to csh and derivatives. The modern portable syntax is
DISPLAY=:0.0
though if your shell is csh or tcsh, this will not work, and you do need setenv after all.
Depending on the use case, you may need to export DISPLAY as well.
Environment variables are a simple way to pass configuration information between programs. The DISPLAY variable indicates to graphical programs on which screen or graphical terminal to display their GUI.

For X Window System, it is a server/client architecture, usually, server side is called display, the tutorials means you should launch server side and specify launch parameter for server side.
read here for more details.

Xterm is just a terminal. And setenv, is used to set Environment variables, which are basically variables used to define the behavior of the terminal. For example, you have the variable PATH, which is used by the terminal to find the path where to execute binaries. Because if you type the command "ls", your terminal has to go into the "env", look for the variable "PATH", and use the value stored in this variable "PATH" to find the path of the ls binary. But I don't know if it's necessary in your situation, could you give more details about the context?

Related

How to ADD an environment variable that can be used by other process in Linux?

In a graphical DE, like KDE, what command can be used to add a new environment variable that can be used by any other process?
Note:
1) I'm aware of export A=B, but it only works for subsequent processes started in the same shell that executed the export, processes started else where, like a graphical application such as Chrome, won't be aware of the export.
2) I'm also aware that you can put it into ~/.bash_profile or alike, but that would need a restart/relogin for the setting to take effect.
Is there something like export but have effect for all applications and doesn't require a significant restart?
Your assumption that you need to restart after placing a variable definition (whether through an export statement or otherwise) in ~/.bash_profile, is flawed. You only need to source the file again after making modifications:
source ~/.bash_profile
or the more portable version:
. ~/.bash_profile
Either statement will (re)load any definitions in that file into your current shell. Sourcing is not the same as executing the script: it will modify the environment in the calling shell itself, not a subshell running the script.
A file like ~/.bash_profile may have many other definitions and settings in it that will mess with the shell. It is better to create a small (temporary) snippet with just the variables you want, and source that instead, as #JeremiahMegel suggests.
If you want to change the environment for a single process you run from the command line, you can set the variables on the same command line:
VAR=value /usr/bin/gedit
This will run gedit with the environment variable VAR set to value, but only for that one child process.
Unfortunately, your desktop applications are a bit more static than that. Most of the graphical applications you see in the menus are probably going to be represented by .desktop files in a folder like /usr/share/applications. These files are run in an environment that has almost none of the variables you are expecting. They rely on absolute paths, and most of the configuration is done by pointing the .desktop file to a script that performs its own setup. You can modify some of these files on an individual basis if you absolutely have to, but I would not recommend doing that. If you do insist on messing around with the graphical apps on your desktop, I would recommend making a copy of the desktop files you plan to modify in to ~/.local/share/applications, or whatever the equivalent is on your system. Those files will override anything found in /usr/share/applications and will only affect you.

Is there a way to detect the terminal program that someone is using in bash

I'm trying to make a bash script that behaves differently based on the terminal program that someone is using (Putty, mobaxterm, etc). Is there any way to retrieve this kind of information from a bash script?
I was searching around online but I was not able to find anything (or I'm just wording it incorrectly, which could be a distinct possibility).
thank you
You need to understand that these are terminal emulators. There is a simple, crude identification function for (most?) modern line terminals (now that's an oxymoron!) but it will return something like vt102 or xterm i.e. whatever the emulator is emulating; not the identity of the program performing this emulation. Incidentally, this is usually used when the session is initiated, and reflected in the value of the $TERM environment variable.
The common way to do this is the environment variable TERM. Try
echo $TERM
That should output the terminal emulator's type, indicating its capabilities. Often, though not always, the value of TERM will be the name of the terminal emulator. For example, xterm may set TERM to xterm, or to xterm-color, depending on configuration.
Note that the user can change this variable, so it may contain something completely different.
That said, if you want to do fancy things with the terminal from a script, you don't need to build support for different terminals by hand. There are various libraries that offer all the usual functions (clear terminal, resize window, change font etc.). The most popular one is terminfo; there are various packages that build on terminfo.

Does anybody know where is LD_PRELOAD variable, in which file?

Just as the title said, I don't know where does this variable locate. I just know how to change it by typing:
$ export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libXpm.so.4
Then is it possible to change it in its file?
LD_PRELOAD is an environment variable (part of the "environment" defined by the C library and Unix conventions). That specific variable tells the dynamic linker how to behave.
It is probably not set to anything by default. If you want to give it a default value every time you log in or start up a shell, you can put that export statement in your .profile or .bashrc file (or whatever the equivalent is for your shell of choice). There's probably also a place you could set it in /etc that would apply to all logins or shells started on your system (if you need it to be set for other users too).
If you only need to set it for a specific program though, that may be overkill. Instead, you might want to write a short shell script to set the environment variable up first, then launch the program. E.G.:
#!/bin/bash
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libXpm.so.4
~/my_program_that_needs_a_special_library

Set variables versus env variables Linux(debian) Bash shell

When creating set variables, what is the advantage of adding them to environment? Both are lost at session termination. I know obviously I can add useful stuff to .bashrc that I would want to access via my user.
Just not sure what the advantages would be? how common practice is it? do I need to worry about it? I am talking real life administration uses.
It seems to me like set is a local variable within a class. ENV is obviously global. Maybe the only use is when traversing different shells? How practical is it?
Environment variables are inherited by any process invoked by your shell. That includes both sub-shells and any other commands you invoke.
(Non-environment) shell variables are not.
For example, $TERM needs to be exported, because commands you invoke from your shell (a text editor, a pager, anything that uses a full-screen text display) needs to know what kind of terminal you're using, so it can look up its capabilities in termcap or terminfo. $LANG and similar variables need to be exported so that commands know about the current locale. $PATH needs to be exported so that any commands you invoke can then invoke other commands.
$PS1, the shell prompt, does not need to be exported, because it's relevant only to the current shell (and is typically initialized for new shells).

what is the meaning of shell title?

I'm new to linux. And I'm confused with the different shell titles in linux.
for example, when I called "/bin/sh", I saw
sh-3.1#
but when I used "su root", I saw
my-environment:~#
and when I logged in as normal user, I saw
user#my-environment:$
I kind of notice # means root and $ means non-root, but could some explain how I got the above titles?
Thanks a lot!
The text at the shell prompt is controlled by the variable $PS1 (and sometimes $PS2 $PS3 $PS4). This blogpost explains how you can customize the prompt in many ways.
Do echo $PS1 in the different terminals to see the current value.
This depends on your variable environment $PS1.
To see what I mean, try doing PS1=something, and see what happens.
To reset it you could do source ~/.bashrc.
So, why do you have different prompt according? Because you don't load the same files according to the command you type to open your sessions, so you end up with different configs, hence a different $PS1

Resources