Command substitution in BASH

BASH has the ability to execute a command string and replace that string with the output of the command. Or to say it another way, the output of a command will replace the command itself.
There are 2 ways to accomplish this.
The first is to surround the command with backticks or blockquotes.
`command`
You can also substitute with [...]

Find IP address from remote end of a TCP socket

In C or C++ it is fairly simple to find the IP address of the remote end of a TCP socket.
The following example shows how to do this using the getpeername() and inet_ntoa() system calls.

int sockfd;
int len;
char * hostip;
struct sockaddr_in sin;

len = sizeof(sin);

if (0 != getpeername(sockfd, (struct sockaddr *) &sin, &len))
[...]

Delete a specific line from a text file with sed

At some point you may have the need to remove all lines within a text file that match a certain pattern. Accomplishing this is easy with the sed command.
Here is the command format.

$ sed -i ‘/PATTERN/ d’ file.txt

The ‘-i‘ option allows you to edit the specified file in place.
PATTERN is a regular expression
d [...]

Display hostname and IP address in C

Here is a good way to determine the hostname and IP address of the local machine in C.
You first have to grab the hostname with gethostname().

char hostbuf[256];
gethostname(hostbuf,sizeof(hostbuf));

Take the hostname and use it to grab the hostent struct with gethostbyname().

struct hostent *hostentry;
hostentry = gethostbyname(hostbuf);

Finally you have to take the hostent that is returned and pull out [...]

Single versus double quotes in BASH

Understanding the difference between double versus single quotes is important when using BASH. Many times you may have seen them being used interchangeably. The basic difference is that variable names will be expanded within double quotes but not within single ones.
This example shows the normal output with no quotes.

$ echo $USER

ryan

As you can see [...]

Format output using printf

printf and its family of functions allow you to easily format string data. There are so many formatting options that it is easy to miss some of its more powerful functionality. Here is a rundown of the more useful features and some examples.
The basic printf format.

printf(format string, format parameter list);

The format string is composed [...]