Everytime I need to access ifconfig , I find using using /sbin/ifconfig. If I need to directly type ifconfig in bash and get the required output without calling the directory every time, what should I do?
121747
Compare Debian (left) and Ubuntu (right):  $ ifconfig                                 $ ifconfig bash: ifconfig: command not found          eth0     Link encap ... $ which ifconfig                           $ which ifconfig $                                          /sbin/ifconfig   Then as superuser:  # ifconfig                                 # ifconfig eth0      Link encap ...                   eth0     Link encap ... # which ifconfig                           # which ifconfig /sbin/ifconfig                             /sbin/ifconfig   Furthermore:  # ls -l /sbin/ifconfig                     # ls -l /sbin/ifconfig -rwxr-xr-x 1 root root 68360 ...           -rwxr-xr-x 1 root root 68040 ...   It seems to me the only reason I cannot run ifconfig without superpowers on Debian is that it's not in my path. When I use /sbin/ifconfig it does work.   Is there any reason I should not add /usr/local/sbin:/usr/sbin:/sbin to my path on Debian? This is a personal computer, I am the only human user.    Versions used (uname -a): Ubuntu:   Linux ubuntu 3.13.0-51-generic #84-Ubuntu SMP Wed Apr 15 12:08:34 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux   Debian:  Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u3 (2015-08-04) x86_64 GNU/Linux
71966
I have a bash application that is producing some result, and I'd like to echo the result to either stdout or to a user chosen file. Because I also echo other interactive messages going to the screen, requiring the user to explicitly use the &gt; redirection when he wants to echo the result to a file is not an option (*), as those messages would also appear in the file.  Right now I have a solution, but it's ugly.  if [ -z $outfile ] then     echo "$outbuf"    # Write output buffer to the screen (stdout) else     echo "$outbuf" &gt; $outfile  # Write output buffer to file fi   I tried to have the variable $outfile to be equal to stdout, to &amp;1 and perhaps something else but it would just write to file having that name and not actually to stdout. Is there a more elegant solution?  (*) I could cheat and use stderr for that purpose, but I think it's also quite ugly, isn't it?
35087
In scripts, first line should specify the path to interpreter. But on different servers Linux, Unix, or BSD this path could be different.  What is more preferable?  #!/usr/bin/env bash    or  #!/bin/bash
44984
Is there any way to debug bash script without using echo and logging?  I'm talking about using breakpoints and stuff like that.
50133
I would find it very convenient to be able to use cd with a file argument.  cd myDirectory/anyname.anyExtension would be equivalent to cd myDirectory/  What would be the best alias or function to achieve this behavior ?  EDIT: Sorry I didn't mention it in the 1st place: I use zsh
26761
I was viewing my .bash_profile file, and I saw this written inside a block of if statement:  if [ -f ~/.bashrc ]; then        . ~/.bashrc    Can someone explain what the second line is actually doing?
45364
I have seen the following technique used many times on many different shells, to test if a variable is empty:  if [ "x$1" = "x" ]; then      # Variable is empty fi   Are there any advantages on using this over the more canonical if [ -z "$1" ]? Could it be a portability issue?
8916
I'd like to know if there is a way to run  program/shell script without typing full path:  /opt/idea/bin/idea.sh
70828
I want to do:  echo "something" &gt;&gt; /etc/config_file  But, since only the root user has write permission to this file, I can't do that. But the following also doesn't work.  sudo echo "something" &gt;&gt; /etc/config_file   Is there a way to append to a file in that situation without having to first open it with a sudo'd editor and then appending the new content by hand?
28963
I thought this would be simple - but it is proving more complex than I expected.  I want to iterate through all the files of a particular type in a directory, so I write this:  #!/bin/bash  for fname in *.zip ; do    echo current file is ${fname} done   This works as long as there is at least one matching file in the directory. However if there are no matching files, I get this:  current file is *.zip   I then tried:  #!/bin/bash  FILES=`ls *.zip` for fname in "${FILES}" ; do     echo current file is ${fname} done   While the body of the loop does not execute when there are no files, I get an error from ls:  ls: *.zip: No such file or directory   How do I write a loop which cleanly handles no matching files?
65652
In bash, I know that it is possible to write a for loop in which some loop control variable i iterates over specified integers.  For example, I can write a bash shell script that prints the integers between 1 and 10: #!/bin/bash  for i in {1..10} do  echo $i done  Is it possible to instead iterate over a loop control variable that is a string, if I provide a list of strings?  For example, suppose that I have a string fname that represents a file name.  I want to call a set of commands for each file name.  For example, I might want to print the contents of fname using a command like this: #!/bin/bash  for fname in {&quot;a.txt&quot;, &quot;b.txt&quot;, &quot;c.txt&quot;} do  echo $fname done  In other words, on the first iteration, fname should have the value fname=&quot;a.txt&quot;, while on the second iteration, fname should have the value fname=&quot;b.txt&quot;, and so on.  Unfortunately, it seems that the above syntax is not quite correct.  I would like to obtain the output:  a.txt b.txt c.txt  but when I try the above code, I obtain this output:  {a.txt, b.txt, c.txt}  Can you please help me determine the correct syntax, so that I can iteratively change the value/contents of the variable fname?  Thank you for your time.
51944
Why do some bash built-ins (such as dirs, pushd or help) don't have manual entries?
98935
I have a sample script (credit goes to ):  #!/bin/bash cat &lt;(cat /etc/lsb-release)   I save this script as somename.sh in my home directory.  Now, I try to open this file in three different ways:  sh somename.sh  bash somename.sh  ./somename.sh   So, I have two questions:   Why are the outputs for the above commands different though they are running the same script?   sh produces a Syntax error bash outputs the desired result ./ gives a Permission Denied error  Why is it necessary to give the script executable permission only when running the script using ./ and not necessary in the other cases?   Thanks in advance!  EDIT: The first part might be similar to the  linked one but, I did have a second question too.
38271
I am always really hesitant to mess around with $IFS because it's clobbering a global.   But often it makes loading strings into a bash array nice and concise, and for bash scripting, conciseness is hard to come by.   So I figure it might be better than nothing if I try to "save" the starting contents of $IFS to another variable and then restore it immediately after i am done using $IFS for something.   Is this practical? Or is it essentially pointless and I should just directly set IFS back to whatever it needs to be for its subsequent uses?
66840
I saw the following snippet in this thread:   sleep 2 if (-x /usr/local/bin/bash) then   exec /bin/sh -c '. ~/.profile; exec /usr/local/bin/bash' endif   My understanding is that if you place this snippet in ~/.login it will invoke bash when you login.  I have a similar situation where I would like to use a similar snippet to invoke a  version of zsh that is different from the default one with which I log in. Part of the reason why I am going through this trouble is because I can't choose my desired version of zsh in the options allowed in chsh, and I don't have administrator privileges.  With this:   How would I change the snippet above to do this when switching between different versions of zsh?  Why are two exec commands needed in the snippet above? What do they do? Also, why does the snippet above use sh and bash (the user is supposed to login with csh)
26834
As far as I understand bash is the default shell in Ubuntu, so why does /bin/sh point to /bin/dash?
20132
I was just trying to list all directories and files under current directory and also write if they are file or directory with the following command:  find -exec echo `echo {} : ;if [ -f {} ]; then echo file; else echo directory;fi` \;   I know it is a silly command, I can use other things like -type f or -type d, but I want to learn why that piece of code did not work as I expected. It just prints directory to all of them. For example while output of find is:  . ./dir ./dir/file   output of my code is :  . : directory ./dir : directory ./dir/file : directory   And output of   echo `echo dir/file : ;if [ -f dir/file ]; then echo file; else echo directory;fi`   is  dir/file : file   I am working on Ubuntu 14.10 and using find (GNU findutils) 4.4.2