LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-08-2021, 11:56 AM   #1
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
"script" doesn't seem to fully recognize one's environment


I'm working on a project that, sadly, involves software that seems to have been written by people who love typing long commands (and don't mind correcting the inevitable typos). As a result, some of us have created aliases to reduce the maddening amount of typing that's necessary to execute commands.

Now... as part of a request to "show me how to do this" I tried running a series of these long commands via their aliases and log the session using "script". Oops! "script" doesn't know about all of my environment. Well, by "my environment" I mean the normal collection of shell variables as well as the aliases. That seems to be too expansive of a definition as "script" is unaware that aliases have been defined. I've been using that utility for years (starting on big iron Unixen) and never noticed this before.

Is this normal "script" behavior? -- I see no switches that might help me out.

Is there a workaround? -- The only one that comes to mind is to bite the bullet and write a short shell script that uses the long version of the commands and run that during the "script" session. One other possibility is to source my ".profile" when first logging the session. (Works but sort of a pain.)

Mostly curious if anyone else has run into this and whether there's a clever fix.

TIA...

Hope everyone's set to enjoy the holidays.
 
Old 12-08-2021, 12:17 PM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
No, this is not normal. script inherits your environment including aliases (unless script itself was aliased to something else). Just tested with script from util-linux 2.23.2 (CentOS 7), 2.32.1 (CentOS 8), and 2.34 (Ubuntu 20.04 LTS).

Last edited by shruggy; 12-08-2021 at 12:23 PM.
 
Old 12-08-2021, 10:55 PM   #3
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by shruggy View Post
No, this is not normal. script inherits your environment including aliases (unless script itself was aliased to something else).
Interesting. I'm running CentOS 8 via Oracle's VirtualBox and script is from util-linux 2.32.1. I'll have to look a tad deeper to see what's going on. May be time for strace though I'm unsure if I'd be able to interpret the results without having the script source code handy.

Thx for the reply.
 
Old 12-09-2021, 04:01 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
aliases are not inherited by default. see: shopt -s expand_aliases
 
Old 12-09-2021, 05:07 AM   #5
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Hmm. Strange.
Code:
$ shopt|grep expand_aliases
expand_aliases 	on
But
Code:
$ grep expand_aliases .bash* /etc/bash*
.bash_history:shopt|grep expand_aliases
Looks like expand_aliases is enabled by default. From the GNU Bash manual:
Quote:
expand_aliases
If set, aliases are expanded as described below under Aliases. This option is enabled by default for interactive shells.

Last edited by shruggy; 12-09-2021 at 05:31 AM.
 
Old 12-09-2021, 08:22 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
for interactive shells, but for scripts executed by cron/ssh/whatever this option is not set (by default).
 
Old 12-09-2021, 09:11 AM   #7
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
script is supposed to be executed interactively.
 
Old 12-10-2021, 12:29 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by shruggy View Post
script is supposed to be executed interactively.
You know, aliases are lost. So the mentioned shopt was not set. Actually I (we) don't know the real execution environment, therefore we could only guess, but I don't want to.

From the other hand better to use functions instead of aliases, that is more convenient (and should be exported too).
 
Old 12-11-2021, 10:10 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Neither functions nor aliases are environment, hence not inherited by child processes.

In bash functions (but not aliases) can be exported.
This is a trick though, working in few OS and requires coding/decoding to/from environment.

If you have defined your aliases in .bashrc then try to source it again after invoking script.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] setting an environment variable - it doesn't seem to work mstrimel Linux - General 5 04-25-2021 05:01 PM
[SOLVED] DigitalEditions doesn't seem to recognize my connection to the internet (Wine/multilib) josiah Slackware 3 12-31-2017 01:19 AM
brctl doesn't seem to be fully communicating internally from network namespace d.custer91 Linux - Networking 1 05-27-2015 02:11 PM
Help With Java Problem Please"""""""""""" suemcholan Linux - Newbie 1 04-02-2008 06:02 PM
GNU doesn't seem to recognize libstdc++ MasterOfTheWind Linux - Newbie 6 10-16-2005 01:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration